在我的网页上,我有一个 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/