我想获取选中的多个复选框的id
HTML:
<input type="checkbox" class="a" id="1">1
<input type="checkbox" class="a" id="2">2
<input type="checkbox" class="a" id="3">3
<input type="checkbox" class="a" id="4">4
<input type="checkbox" class="a" id="5">5
<input type="button" value="Button" id="button">
JS:
$("#button").live('click',function(){
var a = document.getElementsByClassName("a");
alert(a);
alert(a.checked);
});
最佳答案
获取选中的id
:
$('.a').filter(function(){
return this.checked // Takes only checked checkboxes.
}).map(function(){
return this.id // Makes an array which its elements are the ids.
}).get(); // Returns the array.
请注意,根据 w3c 规范,以数字开头的 ID 无效!
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
检查复选框:
除非你的 jQuery 版本是 <1.4.4
,否则不要使用live
$("#containerId").on('click','#button', function(){
$('.a').prop('checked', true);
});
关于javascript - 如何获得多个选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10795947/