javascript - 如果表格列中的所有复选框均已选中,请选中标题复选框

标签 javascript jquery

我的表格如下所示:

enter image description here

如果我选中/取消选中标题(创建、更新、删除),它会选中/取消选中列复选框。这个功能运行良好。但我也想实现相反的目标。

例如,在页面加载时,如果“删除”列中的所有复选框均已选中,则还要检查标题。我如何实现这一目标?

JS

$("#objPermissionTableContent th input[type='checkbox']").on("change", function() {
  var cb = $(this),
    th = cb.closest('th'),
    col = th.index() + 1;
  $("#objPermissionTable td:nth-child(" + col + ") input").prop("checked", this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table" id="objPermissionTableContent">
  <thead class="thead-light">
    <tr class="table-header">
      <th scope="col" class="border-top-0">
        <div class="form-group form-check mb-0">
          <input type="checkbox" class="form-check-input">
          <label class="form-check-label" for="objCreate">Create</label>
        </div>
      </th>
      <th scope="col" class="border-top-0">
        <div class="form-group form-check mb-0">
          <input type="checkbox" class="form-check-input">
          <label class="form-check-label" for="objUpdate">Update</label>
        </div>
      </th>
      <th scope="col" class="border-top-0">
        <div class="form-group form-check mb-0">
          <input type="checkbox" class="form-check-input">
          <label class="form-check-label" for="objRemove">Remove</label>
        </div>
      </th>
    </tr>
  </thead>
  <tbody class="disabled" id="objPermissionTable">
    <tr id="session">
      <td class="col-create" id="16">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-update" id="17">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-remove" id="18">
        <div class="form-check"><input class="form-check-input" type="checkbox" checked></div>
      </td>
    </tr>
    <tr id="scheduledmessage">
      <td class="col-create" id="265">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-update" id="266">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-remove" id="267">
        <div class="form-check"><input class="form-check-input" type="checkbox" checked></div>
      </td>
    </tr>
    <tr id="userpaymentmethod">
      <td class="col-create" id="277">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-update" id="278">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-remove" id="279">
        <div class="form-check"><input class="form-check-input" type="checkbox" checked></div>
      </td>
    </tr>
    <tr id="hw_message_prescription">
      <td class="col-create" id="268">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-update" id="269">
        <div class="form-check"><input class="form-check-input" type="checkbox"></div>
      </td>
      <td class="col-remove" id="270">
        <div class="form-check"><input class="form-check-input" type="checkbox" checked></div>
      </td>
    </tr>
  </tbody>
</table>

<强> JS FIDDLE

最佳答案

在事件处理程序中添加 -

var checkedNum = $('#objPermissionTableContent tbody tr td').eq(col).find('input:checked').length;
var rowsNum = $('#objPermissionTableContent tbody tr').length;
if (checkedNum === rowsNum) {
  // check the header
}

关于javascript - 如果表格列中的所有复选框均已选中,请选中标题复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478627/

相关文章:

javascript - JBusinessDirectory 在搜索表单上拆分类别和子类别

javascript - 我如何用更少的 Javascript 行来表达这一点?

javascript - 当输入类型编号为字符串时,ng-model 会抛出错误 AngularJS

jquery - 在移动/桌面模式之间切换时如何启用/禁用某些 css 属性?

jquery - 以编程方式禁用按钮

javascript函数不返回预期的几何序列

javascript - 你如何使用 javascript 将 html 元素移动到随机位置?

javascript - 鼠标点击页面上的其他地方(不是在特定的 div 上)

javascript - 使用 setTimeout 从创建 setTimeout 时恢复变量值?

javascript - 有没有办法使用 javascript 从图像 src url 获取数字?