如何使用javascript检查没有Id/Class的复选框?如何使用javascript检查没有Id/Class的复选框?
<form>
<input type="checkbox" name="vehicle" value="Bike">bike
<input type="checkbox" name="vehicle" value="Car">car
</form>
如何使用javascript检查没有Id/Class的复选框?如何使用javascript检查没有Id/Class的复选框?
<form>
<input type="checkbox" name="vehicle" value="Bike">bike
<input type="checkbox" name="vehicle" value="Car">car
</form>
在javascript中:
可以使用document.querySelectorAll("input[type='checkbox']")[0]
对于第二个的第一个和document.querySelectorAll("input[type='checkbox']")[1]
。
进行检查,所有你需要使用document.querySelectorAll("input[type='checkbox']")
换句话说:
First_checkbox = document.querySelectorAll("input[type='checkbox']")[0];
Second_checkbox = document.querySelectorAll("input[type='checkbox']")[1];
有些事情,你可以用document.querySelectorAll("input[type='checkbox']")
标签做:
document.querySelectorAll("input[type='checkbox']")[1].checked = true;
document.querySelectorAll("input[type='checkbox']")[1].checked = false;
在jQuery中:
第一个可以使用$(":checkbox")[0]
,第二个可以使用$(":checkbox")[1]
。
进行检查,所有你需要使用$(":checkbox")
换句话说:
First_checkbox = $(":checkbox")[0];
Second_checkbox = $(":checkbox")[1];
有些事情,你可以用$(":checkbox")
标签做:
$(":checkbox")[1].checked = true;
$(":checkbox")[1].checked = false;
可能这是你在找什么getElementsByName()
more on this here。
document.querySelector("input[name='vehicle'][value='Bike']").checked = true
我认为这是自我记录。 Article about attribute selectors。
你想什么选择使用? [大约有十几个与类或id无关](http://api.jquery.com/category/selectors/)。 – Jivings