简体   繁体   中英

Uncheck parent checkbox if all child is unchecked

I've crated a check box group and failed to implement a requirement.

If all checkboxes are unselected within the group, the Top-Level checkbox should be displayed in a unselected state.

I am doing it in a same way as i did it for all selected checkbox. My Third else if statement is not working.

I am trying to fill the three requirement:

  1. If not all checkboxes are selected within the filter grouping, the Top-Level Filter displays ( - ) in the checkbox.

  2. If all checkboxes are selected within the filter grouping, the Top-Level Filter displays selected state with checkmark.

  3. If all checkboxes are unselected within the group, the Top-Level checkbox should be displayed in a unselected state.

I am failed to achieve the last statement.

JS:

 <script>
      $(".faceted-filters").on("click", "#checkAll", function(){

           $(this).parents(".top-lvl-filter").next(".sub-lvl-filter").find("input[type=checkbox]").prop('checked', $(this).prop('checked')); 
           $(this).removeClass("partially-checked"); 

        });

    $("#collapse-one input").change(function(){
        var a = $("#collapse-one input");
        if(a.length !== a.filter(":checked").length){
            $(this).parents(".sub-lvl-filter").prev().find("input").addClass("partially-checked");
        }
        else if(a.length == a.filter(":checked").length){
            $(this).parents(".sub-lvl-filter").prev().find("input").attr('checked','checked');
            $(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
        }
        else if(a.length == a.filter.not(":checked").length){
        $(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
    }


    });
    </script>

Working example: http://codepen.io/anon/pen/PqjJdO

This below change will solve your problem,

$("#collapse-one input").change(function(){
var a = $("#collapse-one input");
if(a.length > a.filter(":checked").length && a.filter(":checked").length > 0){

    $(this).parents(".sub-lvl-filter").prev().find("input").addClass("partially-checked");
}
else if(a.length == a.filter(":checked").length){
    $(this).parents(".sub-lvl-filter").prev().find("input").prop('checked','checked');
    $(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
}
else if(a.filter(":checked").length < 1){

    $(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
    $(this).closest(".composite-filter").find(".top-lvl-filter input").prop('checked','');
}


});

Your first condition is hiding the 3rd condition to happen.

Try this -

$("#collapse-one input").change(function(){
    var a = $("#collapse-one input");

  if(a.filter(":checked").length==0){
        $(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
  $(this).parents(".sub-lvl-filter").prev().find("input").removeAttr('checked','checked');
  }
    else if(a.length !== a.filter(":checked").length){
        //alert('all checked');
        $(this).parents(".sub-lvl-filter").prev().find("input").addClass("partially-checked");
    }
    else if(a.length == a.filter(":checked").length){
        $(this).parents(".sub-lvl-filter").prev().find("input").attr('checked','checked');
        $(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
    }});

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