CheckBoxList检查点击工作时的所有项目

时间:2015-10-07 11:52:28

标签: javascript jquery asp.net

我有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>

感谢。

3 个答案:

答案 0 :(得分:3)

由于CheckBoxList是服务器端控件,因此name属性将与Id一起更改。一种方法是按照@Alex的建议添加一个类。此外,您可以在表格中找到由CheckBoxList呈现的所有复选框,如下所示: -

$(document).ready(function () {
   $("#cbAllYears").click(function () {
       $('input[type="checkbox"]', $("#cblstMonthAvailability")).prop("checked",$(this).prop("checked"));
   });
});

答案 1 :(得分:2)

我建议在lwAWT元素中添加一个类,并使用jquery:

checkbox
$("#cbAllYears").on("click", function() {
  $(".month").prop("checked", $(this).prop("checked"));
});

答案 2 :(得分:0)

试试这个:

 $(document).ready(function () {
     $('[id$=cbAllYears"]').change(function (e) {
       $("input[id^='cblstMonthAvailability']").attr("checked", $(this).is(":checked"));
       return false
     });
 });
相关问题