JQuery checkbox 全选实现

asp.net的全选。

我用了一个html控件 checkbox 。 <input type="checkbox" id="ckAll" runat="server" />全选

和一个服务器控件

 <asp:CheckBoxList ID="chblst" RepeatDirection="Horizontal" runat="server" DataTextField="GradeName" DataValueField="GradeNid"></asp:CheckBoxList>

首先默认全选。

 $(document).ready(
               function () {
                   $("[id^=chblst]").attr("checked", true);
                   $("#ckAll").attr("checked",true);
               }
               );
  $(function () {
               $("#btnSearch").click
               (
                   function () {
                       if ($("#txtTime").val() == "" || $("#txtTime").val().length == 0) {
                           $("#txtTime").focus();
                           alert("时间不能为空!");
                           return false;
                       }
                       return true;
                   }
               );//查找事件 
               $("#ckAll").click
                 (
                   function () {

                       $("input[id^=chblst]").attr("checked", $("#ckAll").attr("checked"));
                   }
                 )
                 ;//选择全选按钮时,所有input类型的id以chblst开头的控件的checked状态与ckAll一致。
$("input[id^=chblst]").click(function (e) { $("input[id^=chblst]").each(function () { if (!$(this).attr("checked")) { $("input[id=ckAll]").attr("checked", false); return false; } else { $("input[id=ckAll]").attr("checked", true); } }); });//所有类型为input的id以chblst开头的对象,点击后,若全部都选择,则cbAll为选中状态,否则为false
});

attr("checked", false),false所在的参数位,jquery的规则是认空字符“”为false,非空字符“false”为true。

也就是说,即使是这样attr("checked", “false”),属性的checked状态也为true,由此可以推出=》attr("checked", 0)为false,attr("checked", 1)为true.

原文地址:https://www.cnblogs.com/pigddyou/p/2619976.html