我有一個(gè)帶有嵌套表和復(fù)選框的頁面,用于檢查所有表格單元格< td>.并且每個(gè)表格單元格的復(fù)選框< td>檢查各個(gè)細(xì)胞.
我想做的是
> CheckAll復(fù)選框選中/取消選中所有父級和子級復(fù)選框
>取消選中父復(fù)選框取消選中CheckALL復(fù)選框并取消選中所有子復(fù)選框
>選中父復(fù)選框選中所有子復(fù)選框,并查看是否選中了所有其他父復(fù)選框,以選中CheckALL復(fù)選框
>檢查子復(fù)選框應(yīng)檢查相應(yīng)的父級
>取消選中子復(fù)選框應(yīng)該檢查兄弟姐妹是否被選中,如果沒有選中,則父checbox應(yīng)該取消選中,如果選中了checkall復(fù)選框,它也應(yīng)該被取消選中.
我無法做到這一點(diǎn).
這是我嘗試過的.
HTML:
<table class="table table-striped tablesorter"> <thead> <tr colspan="2"> <th> <input type="checkbox" id="checkedAll"> </th> <th>Check/UnCheck ALL Boxes</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" class="checkParent"> </td> <td>1KWT</td> </tr> <tr> <td colspan="2" style="padding: 0"> <table class="innertable" style="margin: 10px 0px 10px 50px;border: 1px solid #d0d0d0"> <thead> <tr> <th></th> <th>Sub</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" class="checkChild" style="margin-top: -3px"> </td> <td>1KWT1</td> </tr> <tr> <td> <input type="checkbox" class="checkChild" style="margin-top: -3px"> </td> <td>1KWT2</td> </tr> </tbody> </table> </td> </tr> <tr> <td> <input type="checkbox" class="checkParent" style="margin-top: -3px"> </td> <td>1OMN</td> </tr> <tr> <td colspan="2" style="padding: 0"> <table class="innertable" style="margin: 10px 0px 10px 50px;border: 1px solid #d0d0d0"> <thead> <tr> <th></th> <th>Sub</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" class="checkChild" style="margin-top: -3px"> </td> <td>1OMN1</td> </tr> <tr> <td> <input type="checkbox" class="checkChild" style="margin-top: -3px"> </td> <td>1OMN2</td> </tr> </tbody> </table> </td> </tr> </tbody></table>
腳本:
$(document).ready(function() { $("#checkedAll").change(function(){ if(this.checked){ $(".checkParent, .checkChild").each(function(){ this.checked=true; }); }else{ $(".checkParent, .checkChild").each(function(){ this.checked=false; }); } }); $(".checkParent").click(function () { if ($(this).is(":checked")){ var isAllChecked = 0; $(".checkParent").each(function(){ if(!this.checked) isAllChecked = 1; }) $(".checkChild").prop("checked", true); if(isAllChecked == 0){ $("#checkedAll").prop("checked", true); } }else { $("#checkedAll, .checkChild").prop("checked", false); } }); $(".checkChild").click(function () { if ($(this).is(":checked")){ $(".checkParent").prop("checked", true); }else { $(".checkParent").prop("checked", false); } });});
鏈接到小提琴https://jsfiddle.net/4zhhpwva/
解決方法:
我做到了歡迎改進(jìn).這是更新的腳本
$(document).ready(function() { $("#checkedAll").change(function() { if (this.checked) { $(".checkParent, .checkChild").each(function() { this.checked = true; }); } else { $(".checkParent, .checkChild").each(function() { this.checked = false; }); } }); $(".checkParent").click(function() { if ($(this).is(":checked")) { var isAllChecked = 0; $(".checkParent").each(function() { if (!this.checked) isAllChecked = 1; }) $(this).closest("tr").next("tr").find(".checkChild").prop("checked", true); if (isAllChecked == 0) { $("#checkedAll").prop("checked", true); } } else { $("#checkedAll").prop("checked", false); $(this).closest("tr").next("tr").find(".checkChild").prop("checked", false); } }); $(".checkChild").click(function() { if ($(this).is(":checked")) { var isChildChecked = 0; $(".checkChild").each(function() { if (!this.checked) isChildChecked = 1; }); if (isChildChecked == 0) { $("#checkedAll").prop("checked", true); } $(this).closest("table").closest("tr").prev("tr").find(".checkParent").prop("checked", true); } else { var isAllSiblingChecked = 0; $(this).closest("tr").nextAll("tr").find(".checkChild").each(function() { if ($(this).is(":checked")) isAllSiblingChecked = 1; }); $(this).closest("tr").prev("tr").find(".checkChild").each(function() { if ($(this).is(":checked")) isAllSiblingChecked = 1; }); if (isAllSiblingChecked == 0) { $(this).closest("table").closest("tr").prev("tr").find(".checkParent").prop("checked", false); } $("#checkedAll").prop("checked", false); } });});
我在這里更新了小提琴,看看工作https://jsfiddle.net/shumaise/4zhhpwva/10/
來源:https://www.icode9.com/content-1-420401.html