Check / uncehck checkboxlist with jquery

In most of the examples that I saw on the Internet, there is a fragment in which they use 2 flags, one for one flag (all), and the second - checkboxlist. In my case, I have only one checkboxlist associated with the data source, for example, my data source list options (all, apple, orange, red, blue), I got most of the work, except when everything is not checked, and I check the last element, for example blue, it checks the ALL option. therefore does not work properly. id is the identifier of the Everyone item in the list

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
 $(document).ready(function () {
     var id = "#<%=cbOptions.ClientID %>_0";
     var checkboxlistid = "#<%= cbOptions.ClientID %>";
     $(id).click(function () {
         $("#<%= cbOptions.ClientID %> input:checkbox").attr('checked', this.checked);
     });
     $(checkboxlistid +  " input:checkbox").click(function () {
         if ($(checkboxlistid).attr('value') != 0) {
             if ($(id).attr('checked') == true && this.checked == false) {
                 $(id).attr('checked', false);
             }
             else {
                 if ($(id).attr('checked') == true && this.checked == true)
                     CheckSelectAll();
             }

         }
     });
     function CheckSelectAll() {
         var flag = true;
         $(checkboxlistid + " input:checkbox").each(function () {
             if ($(checkboxlistid).attr('value') != 0) {
                 if (this.checked == false) {
                     flag = false;
                 }
                 else {
                     if ($(id).attr('checked') == true && this.checked == false) {
                         flag = false;
                     }
                     else {
                         flag = true;
                     }
                 }
             }
         });

         $(id).attr('checked', flag);
     }
      });    
         </script>
                <asp:CheckBoxList runat="server" ID="cbOptions" >
                </asp:CheckBoxList>
        </asp:Content>
+3
source share
2 answers
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var id = "#<%=cbOptions.ClientID %>_0";
            var checkboxlistid = "#<%= cbOptions.ClientID %>";
            $(id).click(function () {
                $("#<%= cbOptions.ClientID %> input:checkbox").attr('checked', this.checked);
            });
            $(checkboxlistid + " input:checkbox").click(function () {
            if ($(id).attr('checked') == true && this.checked == false) {
                $(id).attr('checked', false);
            }
            else 
                CheckSelectAll();
            });
            function CheckSelectAll() {
                $(checkboxlistid + " input:checkbox").each(function () {
                    var checkedcount = $(checkboxlistid + " input[type=checkbox]:checked").length;
                var checkcondition = $(checkboxlistid + " input[type=checkbox]:").length - 1
                if (checkedcount >= checkcondition)
                    $(id).attr('checked', true);
                else
                    $(id).attr('checked', false);
                });   
            }
        });    
         </script>
                <asp:CheckBoxList runat="server" ID="cbOptions" >
                </asp:CheckBoxList>
        </asp:Content>
+2
source

, true false "". , . box.id . , . $(.classname)

function CheckSelectAll() {
             var flag = "checked";
             $(checkboxlistid + " input:checkbox").each(function () {
                 if ($(checkboxlistid).attr('value') != 0) {
                     if (this.checked == false) {
                         flag = '';
                     }
                     else {
                         if ($(id).attr('checked') == true && this.checked == false) {
                             flag = '';
                         }
                         else {
                             flag = "checked";
                         }
                     }
                 }
             });

             $(id).attr('checked', flag);
         }
          });  
0

All Articles