【100分】怎么给asp:TreeView控件里的CheckBox的Value赋值,让前台JS可以取到?
如题,页面里有个TreeView控件,其中给每个节点都进行了赋值
TreeNode tempnode = new TreeNode();
tempnode.ShowCheckBox = true;
tempnode.Text = IS.Cate_Name;
tempnode.Expanded = false;
tempnode.Value = IS.Cate_ID;
我在前台:
function checkvalue() {
{
var tree = document.getElementById("treeIS");
var inputs = tree.getElementsByTagName("INPUT");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox" && inputs[i].checked) {
alert(inputs[i].text + " " + inputs[i].value);
}
}
}
}
但是在前台可以找到选中的CheckBox,但是没有给value赋值,只有:<input type="checkbox" name="treeISn1CheckBox" id="treeISn1CheckBox" />
因此我用JS就无法取到,谁有什么好办法????
[解决办法]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeViewSample.aspx.cs" Inherits="WebApp.TreeViewSample" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>treeview父子节点联动</title> <script type="text/javascript"> function OnTreeNodeChecked(evt) { var objNode = evt.srcElement ? evt.srcElement : evt.target; if (objNode.checked == true) { alert(getHideValue(objNode)); setParentChecked(objNode); var objID = objNode.getAttribute("ID"); var objID = objID.substring(0, objID.indexOf("CheckBox")); var objParentDiv = document.getElementById(objID + "Nodes"); if (objParentDiv != null && objParentDiv != "undefined") { setChildChecked(objParentDiv); } } else { var objID = objNode.getAttribute("ID"); var objID = objID.substring(0, objID.indexOf("CheckBox")); var objParentDiv = document.getElementById(objID + "Nodes"); if (objParentDiv != null && objParentDiv != "undefined") { setChildUnChecked(objParentDiv); } } if (objNode.tagName && objNode.tagName != "INPUT") { if (objNode.tagName == "IMG") { return true; } else { if (window.event) { window.event.returnValue = false; return false; } else { evt.preventDefault(); evt.stopPropagation(); } } } } function public_GetParentByTagName(element, tagName) { var parent = element.parentNode; var upperTagName = tagName.toUpperCase(); while (parent && (parent.tagName.toUpperCase() != upperTagName)) { parent = parent.parentNode ? parent.parentNode : parent.parentElement; } return parent; } function setParentChecked(objNode) { var objParentDiv = public_GetParentByTagName(objNode, "div"); if (objParentDiv == null || objParentDiv == "undefined") { return; } var objID = objParentDiv.getAttribute("ID"); objID = objID.substring(0, objID.indexOf("Nodes")); objID = objID + "CheckBox"; var objParentCheckBox = document.getElementById(objID); if (objParentCheckBox == null || objParentCheckBox == "undefined") { return; } if (objParentCheckBox.tagName != "INPUT" && objParentCheckBox.type == "checkbox") return; objParentCheckBox.checked = true; setParentChecked(objParentCheckBox); } function setChildUnChecked(divID) { var objchild = divID.children; var count = objchild.length; for (var i = 0; i < objchild.length; i++) { var tempObj = objchild[i]; if (tempObj.tagName == "INPUT" && tempObj.type == "checkbox") { tempObj.checked = false; } setChildUnChecked(tempObj); } } function setChildChecked(divID) { var objchild = divID.children; var count = objchild.length; for (var i = 0; i < objchild.length; i++) { var tempObj = objchild[i]; if (tempObj.tagName == "INPUT" && tempObj.type == "checkbox") { tempObj.checked = true; } setChildChecked(tempObj); } } function getHideValue(node) { var objchild = public_GetParentByTagName(node, "td").getElementsByTagName("input"); for (var i = 0; i < objchild.length; i++) { var tempObj = objchild[i]; if (tempObj.tagName == "INPUT" && tempObj.type == "hidden") { alert(tempObj.value); } } } </script></head><body> <form id="form1" runat="server"> <div> </div> <asp:TreeView ID="TreeView1" runat="server"> </asp:TreeView> </form></body></html>
[解决办法]
alert(inputs[i].nextSibling.innerHTML)
[解决办法]
var tree = document.getElementById("treeview1");
var inputs = tree.getElementsByTagName("INPUT");
for(var i=0;i<inputs.length;i++)
{
if(inputs[i].type=="checkbox" && inputs[i].checked)
{
alert(inputs[i].text + " " + inputs[i].value);
}
}
或
function ShowCheckbox()
{
var TreeView = " <%=TreeView1.ClientID %> "
var checkNode = document.getElementById( "CheckedNode ")
var checkboxs = document.getElementById(TreeView).getElementsByTagName( "INPUT ")
for(i=0;i <checkboxs.length;i++)
{
if(checkboxs[i].type=="checkbox"&&checkboxs[i].checked )
{
s =checkboxs[i].nextSibling.href
if(s.indexOf( "\\ ") == -1)
{
s=s.substr(0,s.lastIndexOf( " ' "))
s=s.substr(s.lastIndexOf( " ' ")+2)
}
else
{
s=s.substr(s.lastIndexOf("\\")+1,s.lastIndexOf("'")- s.lastIndexOf( "\\ ")-1)
}
checkNode.value+=s+", "
}
}
}
[解决办法]
sorry 刚才没仔细调