首页 > 代码库 > JQuery操作TreeView的全选,反选

JQuery操作TreeView的全选,反选

1 <asp:TreeView ID="tv" runat="server" ExpandDepth="2" ShowCheckBoxes="All" 2         ShowLines="True" ImageSet="Msdn" >3     </asp:TreeView>

JavaScript部分代码

  1 $(  2     function () {  3         //顶级选择  4         $("a[id^=‘content_tvt‘]").each(function (i, item) {  5             var v = $(item).attr("href").split(‘\‘‘)[3].replace(/\\\\/g, ‘|‘);  6             $(this).attr("data", v);  7             $($("#" + $(this).attr("id").replace("tvt", "tvn") + "CheckBox")).attr("data", v);  8             var vv = v.split(‘|‘).length;  9             if (vv == 1) { 10                 $(this).attr("href", "#").attr("onclick", "fun1(‘" + $(this).attr("id") + "‘,‘" + v + "‘)"); 11             } 12             else if (vv == 2) { 13                 $(this).attr("href", "#").attr("onclick", "fun2(‘" + $(this).attr("id") + "‘,‘" + v + "‘)"); 14             } else if (vv == 3) { 15                 $(this).attr("href", "#").attr("onclick", "fun3(‘" + $(this).attr("id") + "‘,‘" + v + "‘)"); 16             } 17         }); 18         //操作checkbox 19         $(":checkbox").each(function (i, item) { 20             $(this).attr("onclick", "fun(‘" + $(this).attr("id") + "‘)"); 21         }); 22     }); 23     //操作checkbox 24     function fun(con) { 25         var vv = $("#" + con).attr("data").split(‘|‘).length; 26         if (vv == 1) { 27             fun4(con); 28         } 29         else if (vv == 2) { 30             fun5(con); 31         } else if (vv == 3) { 32             fun6(con); 33         } 34     } 35     function fun4(con) { 36         if ($("#" + con).is(":checked")) { 37             $(":checkbox").attr("checked", "checked"); 38         } 39         else { 40             $(":checkbox").removeAttr("checked"); 41         } 42     } 43     function fun5(con) { 44         var data = http://www.mamicode.com/$("#" + con).attr("data"); 45         var dataArr = data.split(‘|‘); 46         if ($("#" + con).is(":checked")) { 47             $(":checkbox[data^=‘" + data + "‘]").attr("checked", "checked"); 48             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").attr("checked", "checked"); 49             $(":checkbox[data^=‘" + dataArr[0] + "|‘]").each(function (i, item) { 50                 if (!$(this).is(":checked")) { 51                     $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked"); 52                 } 53             }); 54         } 55         else { 56             $(":checkbox[data^=‘" + data + "‘]").removeAttr("checked"); 57             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked"); 58         } 59     } 60     function fun6(con) { 61         var data = http://www.mamicode.com/$("#" + con).attr("data"); 62         var dataArr = data.split(‘|‘); 63         if ($("#" + con).is(":checked")) { 64             $("#" + con).attr("checked", "checked"); 65             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "|" + dataArr[1] + "‘]").attr("checked", "checked"); 66             $(":checkbox[data^=‘" + dataArr[0] + "|" + dataArr[1] + "|‘]").each(function (i, item) { 67                 if (!$(this).is(":checked")) { 68                     $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "|" + dataArr[1] + "‘]").removeAttr("checked"); 69                 } 70             }); 71             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").attr("checked", "checked"); 72             $(":checkbox[data^=‘" + dataArr[0] + "|‘]").each(function (i, item) { 73                 if (!$(this).is(":checked")) { 74                     $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked"); 75                 } 76             }); 77         } 78         else { 79             $("#" + con).removeAttr("checked"); 80             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "|" + dataArr[1] + "‘]").removeAttr("checked"); 81             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked"); 82         } 83     } 84     //操作文字 85     function fun1(id, data) { 86         if ($("#" + id.replace("tvt", "tvn") + "CheckBox").is(":checked")) { 87             $(":checkbox").removeAttr("checked"); 88         } 89         else { 90             $(":checkbox").attr("checked","checked"); 91         } 92     } 93     function fun2(id, data) { 94         var dataArr = data.split(‘|‘); 95         if ($("#" + id.replace("tvt", "tvn") + "CheckBox").is(":checked")) { 96             $(":checkbox[data^=‘" + data + "‘]").removeAttr("checked"); 97             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked"); 98         } 99         else {100             $(":checkbox[data^=‘" + data + "‘]").attr("checked", "checked");101             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").attr("checked", "checked");102             $(":checkbox[data^=‘" + dataArr[0] + "|‘]").each(function (i, item) {103                 if (!$(this).is(":checked")) {104                     $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked");105                  }106             });107         }108     }109     function fun3(id, data) {110         var dataArr = data.split(‘|‘);111         if ($("#" + id.replace("tvt", "tvn") + "CheckBox").is(":checked")) {112             $("#" + id.replace("tvt", "tvn") + "CheckBox").removeAttr("checked");113             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "|" + dataArr[1] + "‘]").removeAttr("checked");114             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked");115         }116         else {117             $("#" + id.replace("tvt", "tvn") + "CheckBox").attr("checked", "checked");118             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "|" + dataArr[1] + "‘]").attr("checked","checked");119             $(":checkbox[data^=‘" + dataArr[0] + "|" + dataArr[1] + "|‘]").each(function (i, item) {120                 if (!$(this).is(":checked")) {121                     $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "|" + dataArr[1] + "‘]").removeAttr("checked");122                 }123             });124             $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").attr("checked", "checked");125             $(":checkbox[data^=‘" + dataArr[0] + "|‘]").each(function (i, item) {126                 if (!$(this).is(":checked")) {127                     $(":checkbox[data=http://www.mamicode.com/‘" + dataArr[0] + "‘]").removeAttr("checked");128                 }129             });130         }131     }132     </script>