如果条件选中所有复选框
问题描述:
只选择具有设置为checked="checked"
的属性的选项,但选中全部。如果条件选中所有复选框
<input class="chxbx" type="checkbox" checked="checked">
<input class="chxbx" type="checkbox">
<input class="chxbx" type="checkbox">
的jQuery:
$(".chxbx").each(function(i, e){
if($(".chxbx").prop("checked", true)){
// this should select only the the input tag with
//checked="checked" but it selects all checkboxes
}
})
答
您没有使用正确.each
。
应该是:
$(".chxbx").each(function(){
if($(this).is(":checked")){
// this should select only the the input tag with
//checked="checked" but it selects all checkboxes
}
});
或者:
$(".chxbx:checked").each(function(){
// this should select only the the input tag with
//checked="checked" but it selects all checkboxes
});
问题是什么?我认为你需要'$(“。chxbx:checked”)'参见[':checked' Selector](https://api.jquery.com/checked-selector/) – Satpal
嗨Satpal,如果条件选择所有复选框。在html中,只有第一个输入标记被检查,如果条件需要显示只有第一个标记已被检查,那么jquery会被检查 – Deke