javascript - 如果未选中任何子复选框,如何取消选中 'select-all' 复选框

标签 javascript jquery html

我提供了点击 select-all checkbox 的代码,child 复选框被选中/取消选中。现在,如果我取消选择任何子复选框, “全选”复选框应取消选中。我该怎么做?

$(document).ready(function() {
  $('#select-all').click(function(event) {
    var $that = $(this);
    $(':checkbox').each(function() {
      this.checked = $that.is(':checked');
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="select" style="margin-top:54px; margin-left:10px; margin-right:10px;">
  <table class="w3-table-all w3-centered w3-hoverable">
    <tr class="w3-gray" style="width:100%">
      <th style="padding:20px;"><input id="select-all" class="w3-check w3-left" type="checkbox"><span style="padding:20px;">User name</span></th>
      <th style="padding:15px;">User Id</th>
      <th style="padding:15px;">Designation</th>
      <th style="padding:15px;">Phone No</th>
      <th style="padding:15px;">Address</th>
    </tr>
    <tr>
      <td style="padding:20px;"><input class="w3-check w3-left" type="checkbox"><span style="padding:20px;">mandeep kumbhar</span></td>
      <td style="padding:20px">employee@123</td>
      <td style="padding:20px">security Incharge</td>
      <td style="padding:20px">123456789</td>
      <td style="padding:20px">18/522,Neelkanth CHS,Nehru Nagar,Kurla East,Mumbai-400024</td>
    </tr>
    <tr>
      <td style="padding:20px;"><input class="w3-check w3-left" type="checkbox"><span style="padding:20px;">mandeep kumbhar</span></td>
      <td style="padding:20px">employee@123</td>
      <td style="padding:20px">security Incharge</td>
      <td style="padding:20px">123456789</td>
      <td style="padding:20px">awtry lbduyvetyd jhvdytf ihuusb</td>
    </tr>
    <tr>
      <td style="padding:20px;"><input class="w3-check w3-left" type="checkbox"><span style="padding:20px;">mandeep kumbhar</span></td>
      <td style="padding:20px">employee@123</td>
      <td style="padding:20px">security Incharge</td>
      <td style="padding:20px">123456789</td>
      <td style="padding:20px">awtry lbduyvetyd jhvdytf ihuusb</td>
    </tr>
  </table>
</div>

最佳答案

要在用户取消选中任何复选框时取消选中全选,请尝试以下代码。

$('.checkbox').click(function(){
   if($(this).prop("checked") == false){
   $('#select-all').prop('checked', false);
   }
}

关于javascript - 如果未选中任何子复选框,如何取消选中 'select-all' 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250131/

相关文章:

javascript - 在主干js中访问返回值

html - 视频代码HTML中的youtube网址

html - 即使在缩放时,如何在移动浏览器上获得流畅的换行符?

javascript - 如果鼠标聚焦在新的 div 上,我如何在鼠标悬停事件期间显示持续存在的 div?

javascript - nodejs - 证书链中的错误自签名证书

jquery - 我需要在 jQuery 中的 get 函数之外使用 var

javascript - Ajax 调用 JQuery 中的 MongoDB 查询每个循环都会影响结果的顺序

html - 从导入的 CSS 文件继承属性

javascript - 生成的函数

javascript - 使用javascript计算html表格中的数字