<div id="menu" class="dropdown-menu hold-on-click dropdown-checkboxes pull-right">
<label><input type="checkbox" id="cbox0" data-column="0" name = '1'>1</label>
<label><input type="checkbox" id="cbox0" data-column="0" name = "2'>2</label>
<label><input type="checkbox" id="cbox0" data-column="0" name = '3'>3</label>
<label><input type="checkbox" id="cbox0" data-column="0" name = '4'>4</label>
<label><input type="checkbox" id="cbox0" data-column="0" name = '5'>5</label>
<label><input type="checkbox" id="cbox0" data-column="0" name = '6'>6</label>
</div>
var columns = ['2','5','6'];
我想设置名称存在于 div
下可用的 columns
数组中的所有复选框,以使用 jQuery 进行检查。我在谷歌上搜索并找到了多种解决方案,其中用户检查 div
下可用的所有复选框,但我的问题是 div
内的所有复选框都放置在 label
&我只想选择那些名称出现在columns
数组中的列。
请帮忙。
最佳答案
您可以简单地使用
$('#menu').find(':checkbox').prop('checked', true);
注意:ID 必须是唯一的。
var columns = ['2','5','6'];
$("#menu").find("input[type='checkbox']").filter(function(){
return columns.indexOf($(this).attr('name')) > -1;
}).prop("checked",true);
关于javascript - 使用 jquery 通过将名称与数组匹配来选择 div 下的几个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25745299/