C#里面treeview中,用JavaScript怎么得到的选中子节点的父节点,并且把父节点选中
每个节点前面有个checkBox,我想实现选中子节点时,自动选中父节点。我只实现了取消父节点时,就取消所有子节点.这是我的代码,请大家看看。因为好久没有来百度了,没有积分...
每个节点前面有个checkBox,我想实现选中子节点时,自动选中父节点。
我只实现了取消父节点时,就取消所有子节点.这是我的代码,请大家看看。因为好久没有来百度了,没有积分了,抱歉了,大家,谢谢大家
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 = T;
}
setChildChecked(tempObj,T,false);
} 展开
我只实现了取消父节点时,就取消所有子节点.这是我的代码,请大家看看。因为好久没有来百度了,没有积分了,抱歉了,大家,谢谢大家
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 = T;
}
setChildChecked(tempObj,T,false);
} 展开
1个回答
展开全部
<script language="javascript" type="text/javascript">
function HandleCheckbox()
{
var element = event.srcElement;
if (element.tagName == "INPUT" && element.type == "checkbox")
{
var checkedState = element.checked;
while (element.tagName != "TABLE") // Get wrapping table
{
element = element.parentElement;
}
var parentElement = element;
if(checkedState)
{
CheckParents(element);
}
element = element.nextSibling; //element.tagName = DIV
if (element != null) // If no childrens then exit
{
var childTables = element.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++)
{
CheckTable(childTables[tableIndex], checkedState);
}
}
if(checkedState == false)
{
UnCheckParents(parentElement);
}
}
}
// Uncheck the parents of the given table, Can remove the recurse (redundant)
function CheckParents(table)
{
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
CheckTable(parentTable, true);
CheckParents(parentTable);
}
// Check the parents of the given table, Can remove the recurse (redundant)
function UnCheckParents(table)
{
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
var checkedCount =GetCheckedCount(table.parentElement);
if(checkedCount == 0)
{
CheckTable(parentTable, false);
}
UnCheckParents(parentTable);
}
// Handle the set of checkbox checked state
function CheckTable(table, checked)
{
var checkboxIndex = table.rows[0].cells.length - 1;
var cell = table.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1)
{
checkboxes[0].checked = checked;
}
}
//Get checked children count
function GetCheckedCount(table)
{
var checkedCount = 0;
var element = table.nextSibling;
var childTable = table.getElementsByTagName("TABLE");
for(var tableIndex = 0;tableIndex < childTable.length; tableIndex++)
{
var childTables = childTable[tableIndex];
var checkboxIndex = childTables.rows[0].cells.length - 1;
var cell = childTables.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1 && checkboxes[0].checked == true)
{
checkedCount++;
}
}
return checkedCount;
}
</script>
<asp:TreeView ID="roles" onclick="HandleCheckbox();" runat="server" ShowCheckBoxes="All"
CssClass="noborder" Height="150px" Width="350px" ExpandDepth="1">
</asp:TreeView>
function HandleCheckbox()
{
var element = event.srcElement;
if (element.tagName == "INPUT" && element.type == "checkbox")
{
var checkedState = element.checked;
while (element.tagName != "TABLE") // Get wrapping table
{
element = element.parentElement;
}
var parentElement = element;
if(checkedState)
{
CheckParents(element);
}
element = element.nextSibling; //element.tagName = DIV
if (element != null) // If no childrens then exit
{
var childTables = element.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++)
{
CheckTable(childTables[tableIndex], checkedState);
}
}
if(checkedState == false)
{
UnCheckParents(parentElement);
}
}
}
// Uncheck the parents of the given table, Can remove the recurse (redundant)
function CheckParents(table)
{
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
CheckTable(parentTable, true);
CheckParents(parentTable);
}
// Check the parents of the given table, Can remove the recurse (redundant)
function UnCheckParents(table)
{
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
var checkedCount =GetCheckedCount(table.parentElement);
if(checkedCount == 0)
{
CheckTable(parentTable, false);
}
UnCheckParents(parentTable);
}
// Handle the set of checkbox checked state
function CheckTable(table, checked)
{
var checkboxIndex = table.rows[0].cells.length - 1;
var cell = table.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1)
{
checkboxes[0].checked = checked;
}
}
//Get checked children count
function GetCheckedCount(table)
{
var checkedCount = 0;
var element = table.nextSibling;
var childTable = table.getElementsByTagName("TABLE");
for(var tableIndex = 0;tableIndex < childTable.length; tableIndex++)
{
var childTables = childTable[tableIndex];
var checkboxIndex = childTables.rows[0].cells.length - 1;
var cell = childTables.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1 && checkboxes[0].checked == true)
{
checkedCount++;
}
}
return checkedCount;
}
</script>
<asp:TreeView ID="roles" onclick="HandleCheckbox();" runat="server" ShowCheckBoxes="All"
CssClass="noborder" Height="150px" Width="350px" ExpandDepth="1">
</asp:TreeView>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询