简体   繁体   English

如何选中其下面的所有复选框后选中复选框?

[英]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.

相关问题 如果选中所有子复选框,如何检查父复选框 - How to check parent checkbox if all children checkboxes are checked 选中一个复选框后如何删除其他复选框上的复选框 - how to remove check on other checkboxes when one checkbox is checked 如果取消选中一个复选框,如何取消选中以下所有复选框 - How to uncheck all below checkboxes when one checkbox is unchecked 如果勾选了一个复选框,如何取消选中所有复选框? - How to uncheck all checkboxes, if one checkbox checked? 选中所有子复选框时如何检查父项 - How to check parent when all children checkboxes are checked 选中一个复选框后,选中所有其他复选框 - Check all other checkboxes when one is checked 当我选中所有复选框时如何选择所有复选框 - How to select all checkbox when i checked all check boxes 选中复选框是否已选中任何复选框 - Check checkbox if any of the checkboxes is checked AG-GRID-ANGULAR - 如果选中/取消选中单元格渲染器中的所有复选框,如何选中/取消选中标题组件中的复选框? - AG-GRID-ANGULAR - How to check/uncheck checkbox in header component if all checkboxes in the cell renderers are checked/unchecked? 如何检查 Reactjs 中是否选中了所有复选框? - How to check whether all the checkboxes are checked in Reactjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM