我有一个ASP.NET树视图控件,它有复选框。
使用此jQuery检查父项被选中时的所有子复选框。这段代码工作正常。
<script type="text/javascript">
function CheckboxGroupSelection() {
$('.tree :checkbox').on('change', function () {
var checked = this.checked;
var $elem = $(this).closest('table');
var depth = $elem.find('div').length;
var $childs = $elem.nextAll('table');
$childs.each(function () {
var $child = $(this);
var d = $child.find('div').length;
if (d == depth) {
return false;
}
$child.find(':checkbox').prop('checked', checked);
});
});
}
</script>
现在,问题在于:当父节点折叠然后检查父节点复选框时,逻辑上应选择所有子复选框。但是这个jQuery不能用于这样做,因为jQuery使用HTML标记来查找和检查复选框,并且,当父节点折叠时,我看不到子节点标记。
如何处理?或者这有没有办法使用c#或vb.net的代码隐藏实现这一点?
折叠时的HTML标记:
<div style="font-size: 11px; font-family: Tahoma; font-weight: bold; text-align: left;" class="tree" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Index">
<table cellspacing="0" cellpadding="0" style="border-width:0;">
<tbody><tr>
<td><a href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$Treecontrol_Left_0$Tree_Index','t1730b784-94ca-42d3-80aa-1c9c064e271c')"><img style="border-width:0;" alt="Collapse Clinics" src="../Uploadedimages/System/Static_Images/TreeLineImages/dashminus.gif"></a></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn0CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn0CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext0">Clinics</span></td>
</tr>
</tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
<tbody><tr>
<td><div style="width:20px;height:1px"></div></td><td><a href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$Treecontrol_Left_0$Tree_Index','t1730b784-94ca-42d3-80aa-1c9c064e271c\\726a5894-6495-4bd2-90fa-1c0eb60f9406')"><img style="border-width:0;" alt="Expand GP Clinics" src="../Uploadedimages/System/Static_Images/TreeLineImages/tplus.gif"></a></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn1CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn1CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext1">GP Clinics</span></td>
</tr>
</tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
<tbody><tr>
<td><div style="width:20px;height:1px"></div></td><td><img alt="" src="../Uploadedimages/System/Static_Images/TreeLineImages/t.gif"></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn7CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn7CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext7">Other Clinics</span></td>
</tr>
</tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
<tbody><tr>
<td><div style="width:20px;height:1px"></div></td><td><img alt="" src="../Uploadedimages/System/Static_Images/TreeLineImages/l.gif"></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn8CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn8CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext8">Polyclinics</span></td>
</tr>
</tbody></table>
</div>
答案 0 :(得分:0)
Public Sub tree_TreeNodeCheckChanged(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
If e IsNot Nothing AndAlso e.Node IsNot Nothing Then
If e.Node.ChildNodes.Count > 0 Then
CheckTreeNodeRecursive(e.Node, e.Node.Checked)
End If
End If
End Sub
Private Sub CheckTreeNodeRecursive(ByVal parent As TreeNode, ByVal fCheck As Boolean)
For Each child As TreeNode In parent.ChildNodes
If child.Checked <> fCheck Then
child.Checked = fCheck
End If
If child.ChildNodes.Count > 0 Then
CheckTreeNodeRecursive(child, fCheck)
End If
Next
End Sub