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 = $("#" + con).attr("data");
 45         var dataArr = data.split('|');
 46         if ($("#" + con).is(":checked")) {
 47             $(":checkbox[data^='" + data + "']").attr("checked", "checked");
 48             $(":checkbox[data='" + dataArr[0] + "']").attr("checked", "checked");
 49             $(":checkbox[data^='" + dataArr[0] + "|']").each(function (i, item) {
 50                 if (!$(this).is(":checked")) {
 51                     $(":checkbox[data='" + dataArr[0] + "']").removeAttr("checked");
 52                 }
 53             });
 54         }
 55         else {
 56             $(":checkbox[data^='" + data + "']").removeAttr("checked");
 57             $(":checkbox[data='" + dataArr[0] + "']").removeAttr("checked");
 58         }
 59     }
 60     function fun6(con) {
 61         var data = $("#" + con).attr("data");
 62         var dataArr = data.split('|');
 63         if ($("#" + con).is(":checked")) {
 64             $("#" + con).attr("checked", "checked");
 65             $(":checkbox[data='" + 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='" + dataArr[0] + "|" + dataArr[1] + "']").removeAttr("checked");
 69                 }
 70             });
 71             $(":checkbox[data='" + dataArr[0] + "']").attr("checked", "checked");
 72             $(":checkbox[data^='" + dataArr[0] + "|']").each(function (i, item) {
 73                 if (!$(this).is(":checked")) {
 74                     $(":checkbox[data='" + dataArr[0] + "']").removeAttr("checked");
 75                 }
 76             });
 77         }
 78         else {
 79             $("#" + con).removeAttr("checked");
 80             $(":checkbox[data='" + dataArr[0] + "|" + dataArr[1] + "']").removeAttr("checked");
 81             $(":checkbox[data='" + 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='" + dataArr[0] + "']").removeAttr("checked");
 98         }
 99         else {
100             $(":checkbox[data^='" + data + "']").attr("checked", "checked");
101             $(":checkbox[data='" + dataArr[0] + "']").attr("checked", "checked");
102             $(":checkbox[data^='" + dataArr[0] + "|']").each(function (i, item) {
103                 if (!$(this).is(":checked")) {
104                     $(":checkbox[data='" + 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='" + dataArr[0] + "|" + dataArr[1] + "']").removeAttr("checked");
114             $(":checkbox[data='" + dataArr[0] + "']").removeAttr("checked");
115         }
116         else {
117             $("#" + id.replace("tvt", "tvn") + "CheckBox").attr("checked", "checked");
118             $(":checkbox[data='" + 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='" + dataArr[0] + "|" + dataArr[1] + "']").removeAttr("checked");
122                 }
123             });
124             $(":checkbox[data='" + dataArr[0] + "']").attr("checked", "checked");
125             $(":checkbox[data^='" + dataArr[0] + "|']").each(function (i, item) {
126                 if (!$(this).is(":checked")) {
127                     $(":checkbox[data='" + dataArr[0] + "']").removeAttr("checked");
128                 }
129             });
130         }
131     }
132     </script>

 

转载于:https://www.cnblogs.com/zhangzhongkui/p/3909147.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值