There are a lot of methods on the Internet, and they all have flaws after a try. Finally, I found a code that seems to be relatively small and pleasing to the eye. The test results reported that several functions do not exist, so I set the breakpoint debugging and found useful fields for each attribute.
The following is a summary::
First, to obtain the node with checkbox in the treeview in javascript, you need to set some attributes of the treeview node, which I added in the background code.
Copy codeThe Code is as follows:
TreeNode newNode = new TreeNode ();
NewNode. Text = "showText ";
NewNode. Value = id;
NewNode. NavigateUrl = id; // you can obtain the required value or text value in javascript.
NewNode. ShowCheckBox = true; // display checkbox
NewNode. Expanded = false; // collapse a node
NewNode. Checked = true; // select checkbox
Get the node of the Treeview with Input control: var tree = document. getElementById ("treenameOrid"). getElementsByTagName ("input ");
The above is set to checkbox, so the tree selects items with checkbox. It is different from var treeitem = document. getElementById ("treenameorid"); text and value cannot be directly obtained through traversal;
Obtain the Node Selected by the Checkbox in the Treeview, which is usually used as a judgment condition: if (tree [I]. type = "checkbox" & tree [I]. checked) {content}
Obtain the value required by each node: var s = tree [I]. nextSibling. pathname; I found that this cost a lot of brain cells. To get a value from this attribute, you must set the value of newNode. NavigateUrl =;
Okay, now we have a complete javascript code.:
Copy codeThe Code is as follows:
Var idlist;
Function GeSelectedNode (){
Var getAllNodes = "";
Idlist = new Array (); // instantiate an Array
Var tree = document. getElementById ("treenameOrid"). getElementsByTagName ("input ");
For (var I = 0; I <tree. length; I ++ ){
If (tree [I]. type = "checkbox" & tree [I]. checked ){
Var s = tree [I]. nextSibling. pathname; // you must set newNode. NavigateUrl = the required value to obtain the value from this attribute;
GetAllNodes + = s + '/';
Idlist. push (s); // Add the value to the last entry of the array;
}
}
Alert (tree. length );
Alert (getAllNodes );
}
Now, run it and see if it is the result you need. If not, don't forget to set the breakpoint debugging.