实现目标:
1. 全选的checkbox选中时,子checkbox全部选中。反之,全部不选;
2.子checkbox中,只要有没有被选中的,取消全选checkbox的选中;
3.子checkbox的数量和子checkbox被选中的数量一样时,全选checkbox要被选中;
html代码:
<!-- table头全选 -->
<th>全选 <input type="checkbox" id="selectAll"></th><!-- tbody单选框 -->
<td><input type="checkbox" class="singleCheckBox" data-id="${e.id!}"></td>
jquery代码:
function batchSelectCheckBox() {$("body").on("click", "#selectAll", function () {var checked = this.checked;$(this).closest("table").find("input[type=checkbox]").each(function () {this.checked = checked;});});$("body").on("click", ".singleCheckBox", function () {var totalcheckbox = $(".singleCheckBox").length;var rowCheckbox = $(".singleCheckBox:checked").length;if (rowCheckbox == 0) {$("#selectAll").prop("checked", false);}if (totalcheckbox == rowCheckbox) {$("#selectAll").prop("checked", true);} else {$("#selectAll").prop("checked", false);}});}