Js gets the Node Selected by Treeview (C # select the CheckBox item)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.