小围脖

Try , Try , Never Say Die !

 

JS实现树形复选框级联选中

源码:

 1 function getTreeSiblings(id){
 2     var tr = $("#"+id);
 3     var pId = tr.attr("pId");
 4     var siblings = [];
 5     if(pId != undefined){
 6         $("tr").each(function(i, ele) {
 7             tr = $(ele);
 8             if(tr.attr("pId") == pId){
 9                 siblings.push($(this));
10             }
11         });
12     }
13     return siblings;
14 }
15 function getTreeParent(id){
16     var tr = $("#"+id);
17     var parents = [];
18     parents.push(tr);
19 
20     var pId = tr.attr("pId");
21     if(pId != undefined){parents = parents.concat(getTreeParent(pId));}
22     return parents;
23 }        
24 function getTreeChildren(id){
25     var tr = null;
26     var children = [];
27     if(id != undefined){
28         $("tr").each(function(i, ele) {
29             tr = $(ele);
30             if(tr.attr("pId") == id){
31                 children.push(tr);
32                 children = children.concat(getTreeChildren(tr.find("input:eq(0)").val()));
33             }
34         });
35     }
36     return children;
37 }
38 function chkNode(obj) {
39     var id = obj.value, chkbox = null;
40     $.each(getTreeChildren(id), function(i,v){
41         v.find("input:eq(0)").attr("checked", function(){return obj.checked ? "checked":""});
42     });
43     $.each(getTreeParent(id), function(i,v){
44         if(i > 0){
45             chkbox = v.find("input:eq(0)");
46             chkbox.attr("checked", function(){return getTreeChildrenChecked(chkbox.val()) ? "checked":""});
47         }
48     });
49 }
50 function getTreeChildrenChecked(id){
51     var chked = false;
52     $.each(getTreeChildren(id), function(i,v){
53         chked = v.find("input:eq(0)").attr("checked") == true;
54         if(chked) {return false;}
55     });
56     return chked;
57 }

 

posted on 2012-08-01 14:29  小围脖  阅读(4894)  评论(0编辑  收藏  举报

导航