【问题标题】:Check if All check box in Panel1 are checked to show a Panel2检查是否选中了 Panel1 中的 All 复选框以显示 Panel2
【发布时间】:2023-04-07 18:14:01
【问题描述】:

我有 2 个面板,复选框:在 Panel1 中,如果选中所有复选框,我需要显示 Panel2。 我有一个 HTML 和一个脚本,但它不起作用,我认为我错过了一些东西。 这是我的代码:

<body>
<asp:Panel ID="Panel1" runat="server">
    <table>
        <tr>
            <td>
                <asp:Label ID="Title" runat="server" Text="Enter the Title"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Question1" runat="server" Text="Question1"></asp:Label>
            </td>
            <td>
                <asp:CheckBox ID="CheckBox1" name="cb" runat="server" Checked="false" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Question2" runat="server" Text="Question2"></asp:Label>
            </td>
            <td>
                <asp:CheckBox ID="CheckBox2" name="cb" runat="server" Checked="false" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Question3" runat="server" Text="Question3"></asp:Label>
            </td>
            <td>
                <asp:CheckBox ID="CheckBox3" name="cb" runat="server" Checked="false" />
            </td>
        </tr>
    </table>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server">
    <table>
        <tr>
            <td>
                <asp:Label ID="Question4" runat="server" Text="Question4"></asp:Label>
            </td>
            <td>
                <asp:CheckBox ID="CheckBox4" runat="server" Checked="false" />
            </td>
        </tr>
    </table>
</asp:Panel>
<table>
    <tr>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        </td>
    </tr>
</table>

这是我的脚本,但它不起作用

$(document).ready(function () { 
$('#CheckBox1').removeAttr('checked');
$('#CheckBox2').removeAttr('checked');
$('#CheckBox3').removeAttr('checked');
$('#CheckBox4').removeAttr('checked');  
$('#Panel2').hide(); 
$('#Panel1').change(function () { 
if (this.checked) { 
$('#Panel2').show(); 
} 
else { 
$('#Panel2').hide(); 
} 
}); 
});

请帮忙

【问题讨论】:

  • 我也找到了这个脚本,但哪个更好,为什么它不起作用:$(document).ready(function () { var a = document.getElementsByName("cb"); for (var i = 0; i &lt; a.length; i++) if (a[i].checked) { $('#Panel2').show(); } else { $('#Panel2').hide(); } });

标签: javascript asp.net checkbox show-hide


【解决方案1】:

使用 jQuery,您可以将选中的复选框与 Panel1 中的所有复选框进行比较,如下所示:

if($("#Panel1 input[type=checkbox]:checked").length === $("#Panel1 input[type=checkbox]").length){
    $('#Panel2').show(); 
}else{
    $('#Panel2').hide(); 
}

【讨论】:

    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2013-10-28
    • 2018-03-27
    • 2012-07-13
    • 2016-04-22
    相关资源
    最近更新 更多