简体   繁体   English

jQuery - 选中所有/取消选中所有复选框 - 然后取消选中(或检查)其他复选框

[英]jQuery - check all/uncheck all checkboxes - then unchecking (or checking) additional ones

JQuery 1.9.1 and using IE8 and Firefox as browsers. JQuery 1.9.1并使用IE8和Firefox作为浏览器。

I had posted a question here that hadn't been answered - hoping that by re-formulating the question I may get an answer. 我在这里发布了一个尚未回答的问题 - 希望通过重新制定问题我可以得到答案。

Below is the HTML for a couple rows in a table I am displaying on a web page. 下面是我在网页上显示的表格中的几行的HTML。

<tr>
    <td>
        <label for="RoomNbr2" align="right">&nbsp;&nbsp;Room No 2
            <input type="checkbox" align="left" class="checkall" id="RoomNbr2" name="RoomNbr2">
        </label>
    </td>
    <td>
        <label for="PCNbr203" align="right">&nbsp;&nbsp;203
            <input type="checkbox" align="left" id="PCNbr203" name="PCNbr203" class="RoomNbr2">
        </label>
    </td>
    <td>
        <label for="PCNbr204" align="right">&nbsp;&nbsp;204
            <input type="checkbox" align="left" id="PCNbr204" name="PCNbr204" class="RoomNbr2">
        </label>
    </td>
</tr>
<tr>
    <td>
        <label for="RoomNbr3" align="right">&nbsp;&nbsp;Room No 3
            <input type="checkbox" align="left" class="checkall" id="RoomNbr3" name="RoomNbr3">
        </label>
    </td>
    <td>
        <label for="PCNbr310" align="right">&nbsp;&nbsp;310
            <input type="checkbox" align="left" id="PCNbr310" name="PCNbr310" class="RoomNbr3">
        </label>
    </td>
    <td>
        <label for="PCNbr320" align="right">&nbsp;&nbsp;320
            <input type="checkbox" align="left" id="PCNbr320" name="PCNbr320" class="RoomNbr3">
        </label>
    </td>
    <td>
        <label for="PCNbr340" align="right">&nbsp;&nbsp;340
            <input type="checkbox" align="left" id="PCNbr340" name="PCNbr340" class="RoomNbr3">
        </label>
    </td>
    <td>
        <label for="PCNbr350" align="right">&nbsp;&nbsp;350
            <input type="checkbox" align="left" id="PCNbr350" name="PCNbr350" class="RoomNbr3">
        </label>
    </td>
</tr>

If the user clicks on Room No 2 then all the PC's in Room No 2 (the RoomNbr2 class assigned to the individual PC's) get checked. 如果用户点击2 号房间RoomNbr2检查2号房间 (分配给各个PC的RoomNbr2类)中的所有电脑。 Likewise, if Room No 2 is checked & then gets unchecked, all PCs with that class would follow suit. 同样,如果选中2号房间然后取消选中,则所有具有该类别的电脑都会效仿。

I use this code to monitor the checkall class for the Rooms: 我使用此代码来监控Rooms的checkall类:

$('#mytable').on('click','.checkall', function() {
   $('input.' + this.id).prop('checked', this.checked);
   });

I use this code to monitor individual clicks on the table: 我使用此代码来监控表上的单个点击:

$('#mytable').on('click', function() {
   $('input.' + this.id).prop('checked', this.checked);
    });

What I need to do is to change the status of the Room No 2 (or 3) checkbox when it has been checked and one of the dependent checkboxes becomes unchecked. 我需要做的是更改2号房间 (或3)复选框的状态,当它被选中时,其中一个从属复选框变为未选中状态。

For example: 例如:

If I click Room No 2, the checkbox for Room No 2 becomes checked, as will 203 & 204 . 如果我单击Room No 2,则会选中Room No 2的复选框, 203204也是如此

If I uncheck 203 , then 204 should remain checked, with the checkbox for Room No 2 going to a status that is either unchecked (without altering the status for the checkbox for 204 ) or goes to an indeterminate status (something other than visually showing checked). 如果我取消选中203 ,那么204应保持选中状态, 2号房间的复选框将进入未选中的状态(不更改204的复选框的状态)或进入不确定状态(除了直观显示已检查的状态) )。

At any given time, I know which Machines have been checked: 在任何时候,我都知道哪些机器已经过检查:

$("input[name*=PC]:checked").map(function () {return this.name;}).get().join(",")

which is what I really want to know. 这是我真正想知道的。 I just do not want the Room No checkboxes to indicate they are checked when all dependent checkboxes are no longer checked. 我只是不希望Room No复选框指示在不再检查所有相关复选框时检查它们。 All the examples I find are of an all-checked/all-unchecked nature. 我发现的所有例子都是全部检查/全部未经检查的性质。

Would appreciate any thoughts or direction as to how best to resolve this issue. 将不胜感激任何关于如何最好地解决此问题的想法或方向。

How about setting the checkboxes to indeterminate if they aren't all checked? 如果没有选中它们,设置复选框如何不确定呢?

$(".checkall").prop("indeterminate", true);

A nice writeup on the subject: http://css-tricks.com/indeterminate-checkboxes/ 关于这个主题的一篇很好的文章: http//css-tricks.com/indeterminate-checkboxes/

How about checking the number of checkboxes against the checked number of checkboxes and setting the main checkbox per line to checked, unchecked, or indeterminate? 如何根据选中的复选框数检查复选框的数量,并将每行的主复选框设置为选中,取消选中或不确定?

jsFiddle example jsFiddle例子

$('#mytable').on('click', '.checkall', function () {
    $('input.' + this.id).prop('checked', this.checked);
});
$('#mytable').on('click', 'input:not(.checkall)', function () {
    var total = $(this).closest('tr').find('input:not(.checkall)').length;
    var checked = $(this).closest('tr').find('input:not(.checkall):checked').length;
    if (total == checked) {
        $(this).closest('tr').find('input:first').prop('checked', true);
        $(this).closest('tr').find('input:first').prop('indeterminate', false);
    } else if (checked == 0) {
        $(this).closest('tr').find('input:first').prop('checked', false);
        $(this).closest('tr').find('input:first').prop('indeterminate', false);
    } else {
        $(this).closest('tr').find('input:first').prop('indeterminate', true);
    }
});

What about this strategy? 这个策略怎么样?

$('input[type=checkbox]').on('click', function() {
    var clickedCb = $(this);
    var tr = $(this).closest('tr');
    var mainCb = tr.children('td input')[0]; 
    if (clickedCb == mainCb) { // probably this check is not 100% correct.
        // do the checkall stuff
    } else {
        mainCb.prop('indeterminate', true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM