简体   繁体   中英

How do I uncheck the other selected list items that was previously select after I click on “Select All”?

I have these codes that allow me to disable all other checkboxes when I check on "Select All" however if I previously did check on other checkboxes before checking on "Select All", previously checked boxes does not get removed but only disabled with the boxes still checked. so how can I uncheck the other selected list items that was previously select after I click on "Select All"?

<asp:CheckBoxList ID="Checkboxlist1" runat="server" Height="80px" Width="500px" AppendDataBoundItems="True" ViewStateMode="Enabled">
                    <asp:ListItem Text="Select All" Value="Select All"></asp:ListItem>
                </asp:CheckBoxList> 


  $(function () {
    $("#Checkboxlist :checkbox").change(function () {
        $("#Checkboxlist  :checkbox").click(function () {
            var ischecked = $(this).is(":checked");
            var val = $(this).val();
            //alert(val);
            if (val == "Select All") {
                if (ischecked) {                    
                    $("#Checkboxlist  :checkbox").attr('disabled', 'disabled');
                    $(this).removeAttr('disabled');
                    $(this).attr('checked', true)
                    return;
                } else {                 
                    $("#Checkboxlist  :checkbox").removeAttr('disabled');
                    return;
                }
            }
        });
    });
})

In the else , before return simply remove the disabled from Select All :

$(this).removeAttr("disabled");

OR

just edit your selector for not having the checkbox whose value is Select All :

$("#Checkboxlist1:checkbox[value='Select All']")

Code:

if (val == "Select All") {
    //if Select All is checked, disable all other checkboxes and uncheck them as well
    if (ischecked) {
        $("#Checkboxlist1:checkbox").attr("disabled", "disabled"); //disable all checkboxes
        $("#Checkboxlist1:checkbox").prop('checked', false);  //uncheck all checkboxes          
        $(this).prop('checked', true);  //check the "Select All" checkbox       
        return;
    }
    //enable all other checkboxes and uncheck them too
    else {
        $("#Checkboxlist1:checkbox").removeAttr("disabled"); //enable all checkboxes
        $("#Checkboxlist1:checkbox").prop('checked', false); //uncheck all checkboxes          
        $(this).prop('checked', false);  //uncheck the "Select All" checkbox
        return;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM