我的 checkBoxList 包含月份数,当用户单击“检查所有我有此 HTML”时,我需要检查此列表中的所有项目:
<asp:CheckBox ID="cbAllYears" Text="All Years" runat="server" />
<asp:CheckBoxList ID="cblstMonthAvailability" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Jan" Value="1"></asp:ListItem>
<asp:ListItem Text="Feb" Value="2"></asp:ListItem>
<asp:ListItem Text="Mar" Value="3"></asp:ListItem>
<asp:ListItem Text="Apr" Value="4"></asp:ListItem>
<asp:ListItem Text="May" Value="5"></asp:ListItem>
<asp:ListItem Text="Jun" Value="6"></asp:ListItem>
<asp:ListItem Text="Jul" Value="7"></asp:ListItem>
<asp:ListItem Text="Aug" Value="8"></asp:ListItem>
<asp:ListItem Text="Sep" Value="9"></asp:ListItem>
<asp:ListItem Text="Oct" Value="10"></asp:ListItem>
<asp:ListItem Text="Nov" Value="11"></asp:ListItem>
<asp:ListItem Text="Dec" Value="12"></asp:ListItem>
</asp:CheckBoxList>
这是我的 Jquery 代码,它仅在第一次单击时工作
<script type="text/javascript">
$(document).ready(function () {
$("#cbAllYears").bind("click", function () {
$("INPUT[id^='cblstMonthAvailability']").attr("checked", $(this).is(":checked"));
});
});
</script>
谢谢。
最佳答案
由于 CheckBoxList
是服务器端控件,因此 name 属性将随 Id 一起更改。一种方法是按照 @Alex 的建议添加一个类。此外,您还可以找到由 CheckBoxList 呈现的表格内的所有复选框,如下所示:-
$(document).ready(function () {
$("#cbAllYears").click(function () {
$('input[type="checkbox"]', $("#cblstMonthAvailability")).prop("checked",$(this).prop("checked"));
});
});
关于javascript - CheckBoxList 检查单击时的所有项目仅工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991509/