TreeView中父子节点的checkbox的同时选中问题,js控制.
html:
<asp:TreeView ID="tvLimit" runat="server" ShowCheckBoxes="All" onclick="javascript:GoClick();"
ShowLines="True" ExpandDepth="0"></asp:TreeView>
js:
<script >
function GoClick()
{
var obj = event.srcElement;
if(obj.tagName.toUpperCase() != "INPUT") return;
var tb = obj.parentElement.parentElement.parentElement.parentElement;//当前节点的Table
SelAllChildren(tb,obj.checked);
tb= tb.parentElement;//
while(true)
{
tb = tb.previousSibling
if( tb == null) return;
SetParentCheckBoxStatus(tb);
tb= tb.parentElement
if( tb == null) return;
}
}
function SelAllChildren(tb,bChecked)
{
var div = tb.nextSibling;
try
{
for(var i=0;i<div.children.length;i++)
{
if(div.children[i].tagName.toUpperCase()!="TABLE")continue;
var chkBox= FindRowCheckBox(div.children[i].rows[0]);
if(chkBox == null) continue;
chkBox.checked = bChecked;
SelAllChildren(div.children[i],bChecked);
}
}catch(e){}
}
function SetParentCheckBoxStatus(objTb)
{
var div = objTb.nextSibling;
var bSelAll = true;
var bSelOne = false;
for(var i=0;i<div.children.length;i++)
{
if(div.children[i].tagName.toUpperCase()!="TABLE")continue;
var chkBox= FindRowCheckBox(div.children[i].rows[0]);
if(!chkBox.checked)
{
bSelAll =false;
}else
{
bSelOne = true;
}
}
if(objTb.rows!= null)
{
var chkBox = FindRowCheckBox(objTb.rows[0])
chkBox.checked = bSelOne;
}
}
function FindRowCheckBox(tr)
{
for(var i=0;i<tr.cells.length;i++)
{
if(tr.cells[i].firstChild.tagName == "INPUT")
{
return tr.cells[i].firstChild;
}
}
}
</script>