如何使用复选框数组的id来检查复选框数组中的复选框是否被选中?
我正在使用下面的代码,但它总是返回被选中的复选框的数量,而不考虑id。
``js function isCheckedById(id) { alert(id)。 var checked = $("input[@id=" + id + "]:checked").length; alert(checked)。
如果(checked == 0) {
返回false。
} else {
返回true。
}
}
$('#' + id).is(":checked")
如果复选框被选中,就会得到。
对于一个同名的复选框数组,你可以通过以下方式获得选中的复选框列表。
var $boxes = $('input[name=thename]:checked');
然后循环浏览这些复选框,看看哪些复选框被选中,你可以这样做。
$boxes.each(function(){
// Do stuff here with this
});
要想知道有多少人被选中,你可以这样做。
$boxes.length;
ID在你的文件中必须是唯一的,也就是说,你***不应该这样做。
<input type="checkbox" name="chk[]" id="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" id="chk[]" value="Bananas" />
取而代之的是,丢弃ID,然后通过名称,或通过包含元素来选择它们。
<fieldset id="checkArray">
<input type="checkbox" name="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" value="Bananas" />
</fieldset>
而现在的jQuery。
var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
//there should be no space between identifier and selector
// or, without the container:
var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;
$('#checkbox').is(':checked');
如果复选框被选中,上述代码将返回true,如果没有,则返回false。