0

我有一个带有复选框的 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>
4

1 回答 1

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
于 2013-06-08T06:24:07.410 回答