c# - 通过单击复选框检查列表中的所有复选框

标签 c# javascript asp.net

在我的网页上,我有一个 CheckBoxList 和一个复选框。当我单击复选框时,CheckBoxList 中的所有复选框都应该被选中。我的 CheckBoxList 必须位于 Bodycontent 占位符下,因为这就是网页的布局,并且我将脚本保留在同一占位符中。

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
            function select(ch) {
                var allcheckboxes = document.getElementById('<%=CheckBoxList1.ClientID %>').getElementsByTagName("input");
                for (i = 0; i < allcheckboxes.length; i++)
                    allcheckboxes[i].checked = ch.checked;
            }
        </script>

        <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
            RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem>Item A</asp:ListItem>
            <asp:ListItem>Item B</asp:ListItem>
            <asp:ListItem>Item C</asp:ListItem>
        </asp:CheckBoxList>

            <asp:CheckBox ID="allCheck" onclick="select(this)" runat="server" Text="Select all" />
                <br />
</asp:Content>

上面没有做任何事情。单击复选框后没有任何反应!我已经被这个小问题困扰了很长时间,但无法做到同样的事情。有什么建议吗?

最佳答案

将函数名称更改为其他名称;它会起作用的

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
            function select1(ch) {
                var allcheckboxes = document.getElementById('<%=CheckBoxList1.ClientID %>').getElementsByTagName("input");
                for (i = 0; i < allcheckboxes.length; i++)
                    allcheckboxes[i].checked = ch.checked;
            }
        </script>

        <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
            RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem>Item A</asp:ListItem>
            <asp:ListItem>Item B</asp:ListItem>
            <asp:ListItem>Item C</asp:ListItem>
        </asp:CheckBoxList>

            <asp:CheckBox ID="allCheck" onclick="select1(this)" runat="server" Text="Select all" />
                <br />
</asp:Content>

关于c# - 通过单击复选框检查列表中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9582852/

相关文章:

javascript - React - 处理登录和身份验证的最佳方式是什么?

asp.net - 为选中的 CheckBoxField 设置默认 bool 值

android - 无法从自己的网页下载和安装应用程序 APK

Javascript - 获取上个月的日期

javascript - 使用 setTimeout() 延迟事件

c# - 有没有一种可靠的方法来检测浏览器是否在 C# 中启用了 javascript?

c# - 如何使用 Roslyn 创建基于结构的属性

c# - 在C#中拦截WebBrowser发出的请求

java - C# 尝试从服务器获取 "get"

c# - 如何使用 C# 执行 MySQL 表的校验和?