[英]How to check a checkbox when all the checkboxes below it are checked?
I am dynamically adding tr and td to a table this way.我以这种方式动态地将 tr 和 td 添加到表中。
var arr=[1,2,3];
var trtest = "<tr>";
trtest = trtest +"<tr><td><input type='checkbox' onclick=onAllCheckBoxClick('" + arr+ "') class = 'all' id ='all'>All</td></tr>";
$.each(arr, function(i, tmp) {
trtest = trtest +"<tr><td><input type='checkbox' onclick=onCheckBoxClick('" + this+ "','" + arr+ "') class = 'all' id ='"+tmp+"'/>"+tmp+"</td></tr>";
});
This code is working and I am getting 4 checkboxes this way,此代码正在运行,我以这种方式获得 4 个复选框,
All
1
2
3
Here, when I try to check individual child checkbox's 1,2,3 then All checkbox have to be selected.在这里,当我尝试检查单个子复选框的 1,2,3 时,必须选择所有复选框。
I tried adding this click event to each checkbox this way,我尝试以这种方式将此点击事件添加到每个复选框,
function onCheckBoxClick(checkBoxId,arr){
var chkselected = [];
$('#generateTable').find(':checkbox:checked').each(function() {
chkselected.push(this.id);
});
//
if (arr.split(",").length == chkselected.length && ($('#'+checkBoxId).is(':checked') == true)) {
$('#all').attr('checked',true);
}
//
else if (arr.split(",").length == chkselected.length && ($('#'+checkBoxId).is(':checked') == false)) {
$('#all').attr('checked',false);
}
}
Issue is when I check 1,2, and lastly 3 immediately All check box is getting checked,问题是当我立即检查 1,2 和最后 3 时所有复选框都被选中,
But when I try to check 3,2 and then 1 finally, All check box is not checked.但是当我尝试检查 3,2 和 1 最后时,未选中所有复选框。
I want All to be checked if 1,2,3 are checked and All to be unchecked if any of 1,2,3 is unchecked.如果 1,2,3 被选中,我希望 All 被选中,如果 1,2,3 中的任何一个被取消选中,All 被取消选中。
I am new to jQuery and so can anyone help me in this issue?我是 jQuery 的新手,所以有人可以帮助我解决这个问题吗?
Assign the all of the checkboxes in the "group" with the same name similar to how you would handle radio inputs.为“组”中的所有复选框分配相同的名称,类似于您处理无线电输入的方式。
Then for the select all checkbox assign the same name and also assign some sort of unique item so you can capture the click event on that individual item (in my example i used role="selectall" you can use whatever you want).然后为全选复选框分配相同的名称并分配某种独特的项目,以便您可以捕获该单个项目上的点击事件(在我的示例中,我使用了 role="selectall" 您可以使用任何您想要的)。
<input type="checkbox" id="checkAll" name="chkGroup1" role="selectall" />
<label for='checkAll'>Select All</label>
<br />
<input type='checkbox' id='checkbox1' name='chkGroup1' />
<label for='checkbox1'>product1</label>
<br />
<input type='checkbox' id='checkbox2' name='chkGroup1' />
<label for='checkbox2'>product2</label>
<br />
<input type='checkbox' id='checkbox3' name='chkGroup1' />
<label for='checkbox3'>product3</label>
<br />
<input type='checkbox' id='checkbox4' name='chkGroup1' />
<label for='checkbox4'>product4</label>
Since you have assigned the name you can use it as your jQuery selector and you are assigning the checked property of the input to the test of the items with the same name.由于您已分配名称,因此您可以将其用作 jQuery 选择器,并将输入的 checked 属性分配给具有相同名称的项目的测试。
$('input:checkbox[role=selectall]').click(function () {
$('[name="' + this.name + '"]').prop('checked', this.checked)
})
http://jsfiddle.net/SeanWessell/11h0f2jm/ http://jsfiddle.net/SeanWesell/11h0f2jm/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.