简体   繁体   English

取消选中“全部选中”复选框

[英]Uncheck a “Check all” Checkbox

I made a table that has checkbox at the beginning of the row. 我制作了一个表格,该表格在行的开头具有复选框。 I also made a "Check All" checkbox. 我还做了一个“全部检查”复选框。 When that is "checked", all other checkbox on the table will be checked. 选中该复选框后,将选中表上的所有其他复选框。 That works fine. 很好 But, when I "unchecked" one checkbox on the table, the "Check All" checkbox is still "checked". 但是,当我“取消选中”表上的一个复选框时,“全部选中”复选框仍处于“选中状态”。 How can I uncheck it when one of the other checkboxes changed? 其他复选框之一更改后,如何取消选中它?

I have my code here: 我的代码在这里:

Script: 脚本:

$('#chkall').change(function() {
var checkboxes = $(this).closest('table').find(':checkbox');
if($(this).is(':checked')) {
    checkboxes.prop('checked', true);
} else {
    checkboxes.prop('checked', false);
}
});

Table (html): 表格(html):

<table class="table table-hover">
                <tr>
                    <th><input type ="checkbox" id = "chkall"></th>
                    <th>Type</th>
                    <th>Brand</th>
                    <th>Description</th>
                    <th>Serial No</th>
                    <th>Asset No</th>
                    <th>Date of Purchased</th>
                    <th>Warranty Expiration</th>
                    <th>Status</th>
                </tr>

                <tr>
                    <td><input type ="checkbox"></td>
                    <td>Keyboard</td>
                    <td>HP</td>
                    <td>One time use keyboard</td>
                    <td>123456</td>
                    <td>789456</td>
                    <td>July 5, 2019</td>
                    <td>August 6, 2015</td>
                    <td>Available</td>
                </tr>

                <tr>
                    <td><input type ="checkbox"></td>
                    <td>Keyboard</td>
                    <td>HP</td>
                    <td>One time use keyboard</td>
                    <td>123456</td>
                    <td>789456</td>
                    <td>July 5, 2019</td>
                    <td>August 6, 2015</td>
                    <td>Available</td>
                </tr>

                <tr>
                    <td><input type ="checkbox"></td>
                    <td>Keyboard</td>
                    <td>HP</td>
                    <td>One time use keyboard</td>
                    <td>123456</td>
                    <td>789456</td>
                    <td>July 5, 2019</td>
                    <td>August 6, 2015</td>
                    <td>Available</td>
                </tr>

                <tr>
                    <td><input type ="checkbox"></td>
                    <td>Keyboard</td>
                    <td>HP</td>
                    <td>One time use keyboard</td>
                    <td>123456</td>
                    <td>789456</td>
                    <td>July 5, 2019</td>
                    <td>August 6, 2015</td>
                    <td>Available</td>
                </tr>

                <tr>
                    <td><input type ="checkbox"></td>
                    <td>Keyboard</td>
                    <td>HP</td>
                    <td>One time use keyboard</td>
                    <td>123456</td>
                    <td>789456</td>
                    <td>July 5, 2019</td>
                    <td>August 6, 2015</td>
                    <td>Available</td>
                </tr>
</table>

Try comparing the number of checkboxes with the number of checked checkboxes 尝试比较复选框的数量和已选中的复选框的数量

$('table').on('change', ':checkbox:not(#chkall)', function(){
   var checkboxes = $(this).closest('table').find(':checkbox:not(#chkall)'),
       allchecked = (checkboxes.length === checkboxes.filter(':checked').length);

   $('#chkall').prop('checked', allchecked);
});

This code will also enable the check-all if you manually check ever single checkbox 如果您手动选中单个复选框,此代码还将启用全选功能


Full example 完整的例子

 $('#chkall').change(function() { var checkboxes = $(this).closest('table').find(':checkbox'); if ($(this).is(':checked')) { checkboxes.prop('checked', true); } else { checkboxes.prop('checked', false); } }); $('table').on('change', ':checkbox:not(#chkall)', function() { var checkboxes = $(this).closest('table').find(':checkbox:not(#chkall)'), allchecked = (checkboxes.length === checkboxes.filter(':checked').length); $('#chkall').prop('checked', allchecked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table class="table table-hover"> <tr> <th> <input type="checkbox" id="chkall"> </th> <th>Type</th> <th>Brand</th> <th>Description</th> <th>Serial No</th> <th>Asset No</th> <th>Date of Purchased</th> <th>Warranty Expiration</th> <th>Status</th> </tr> <tr> <td> <input type="checkbox"> </td> <td>Keyboard</td> <td>HP</td> <td>One time use keyboard</td> <td>123456</td> <td>789456</td> <td>July 5, 2019</td> <td>August 6, 2015</td> <td>Available</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>Keyboard</td> <td>HP</td> <td>One time use keyboard</td> <td>123456</td> <td>789456</td> <td>July 5, 2019</td> <td>August 6, 2015</td> <td>Available</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>Keyboard</td> <td>HP</td> <td>One time use keyboard</td> <td>123456</td> <td>789456</td> <td>July 5, 2019</td> <td>August 6, 2015</td> <td>Available</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>Keyboard</td> <td>HP</td> <td>One time use keyboard</td> <td>123456</td> <td>789456</td> <td>July 5, 2019</td> <td>August 6, 2015</td> <td>Available</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>Keyboard</td> <td>HP</td> <td>One time use keyboard</td> <td>123456</td> <td>789456</td> <td>July 5, 2019</td> <td>August 6, 2015</td> <td>Available</td> </tr> </table> 

You can just add this event handler: 您可以仅添加以下事件处理程序:

$('#chkall').closest('table').on('change', ':checkbox:not(#chkall)', function() {
    if (!$(this).is(':checked'))
        $('#chkall').prop('checked', false);
});

You could compare the number of checkboxes with the number of checked checkboxes. 您可以将复选框的数量与选中的复选框的数量进行比较。

If the numbers aren't equal when a change event is fired, uncheck the #chkall element: 如果change事件触发时数字不相等,请取消选中#chkall元素:

Example Here 这里的例子

$('table :checkbox:not(#chkall)').on('change', function () {
  if ($(this).length !== $(this).closest('table').find(':checked:not(#chkall)').length) {
    $('#chkall').prop('checked', false);
  }
});

Add a handler for all the other checkboxes that clears the check-alll box. 为所有其他复选框添加一个处理程序,以清除复选框。

$(":checkbox:not(#chkall)").change(function() {
    if (!this.checked) {
        $("#chkall").prop("checked", false);
    }
});

Try to add another change event to all your checkboxes like this 尝试像这样向您的所有复选框添加另一个更改事件

$('.yourcheckboxclass').change(function() { });

or 要么

$('#yourtable input:checkbox').change(function() { });

inside the function, select your #chkall and uncheck it. 在函数中,选择您的#chkall并取消选中它。

Try something like this: 尝试这样的事情:

 $('#chkall').on("change", function() { if (!$(this).is(':checked')) { $('.selectRow').prop('checked', false); } else { $('.selectRow').prop('checked', true); } }); $('.selectRow').each(function( index ){ $(this).on("change", function(){ if (!$(this).is(':checked')) { $('#chkall').prop('checked', false); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th> <input type="checkbox" id="chkall" >Check All</input> </th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" class="selectRow" /> </td> </tr> <tr> <td> <input type="checkbox" class="selectRow" /> </td> </tr> </tbody> </table> 

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

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