A few days ago, in order to improve the tree-based permission selection function, jquery was used to write a method based on the tree structure. The result of this method is: "When a node is selected, the selected status of its child nodes also changes with the selected status of the current node. When the current node is selected, all its ancestor nodes should also be selected. If the node is canceled, the selected state of its ancestor node should be determined based on whether the same level node is selected ."
Note: The tree is generated using the TreeView control of.
The Code is as follows:
The Code is as follows:
JQuery (function (){
JQuery (": checkbox"). click (function (){
Var objNode = this;
Var objNodeId = objNode. id;
Var pObjId = objNodeId. substring (0, objNodeId. indexOf ("CheckBox") </span> + "Nodes ";
JQuery ('#' + pObjId + 'input [type = "checkbox"] '). each (function (){
This. checked = objNode. checked;
}
);
JQuery ("#" + objNodeId). parents ("p [id]"). each (function (){
Var pId = this. id;
Var cbId = pId. substring (0, pId. indexOf ("Nodes") + "CheckBox ";
Var cbCount = jQuery ('#' + pId + 'input: checked ');
If (objNode. checked | cbCount. length = 0)
{
Var sId = document. getElementById (cbId );
If (sId)
{
SId. checked = objNode. checked;
}
}
}
);
});
});