Jquery check box to select and uncheck the example

  • 2020-03-30 01:07:47
  • OfStack

Function:

A: when you click the checkbox, select all the subcheckboxes, and then click to uncheck all the checkboxes

B: if one child check box is selected, the parent check box is selected. If all children check boxes are not selected, the parent check box is not selected



function selectAll(mainId,klass){
 $("." + klass).each(function(){
     if($("#" + mainId).attr("checked")== "checked"){
      $(this).attr("checked", "checked");
     }
     else{
      $(this).removeAttr("checked");
     }
 });
}

The above implementation selects and unchecks all subboxes, and the implementation of the data receives an array of check boxes in the controller



function checkSonCheckBox(father,son){
 $("."+son).click(function(){
  if($(this).attr("checked")== "checked"){
   $(this).addClass("checked");
  }else{
   $(this).removeClass("checked");
  }
  if($("."+son).hasClass("checked")){
   $("#"+father).attr("checked","checked");
//      Console.log (" at least one child check box selected!" );
  }else{
   $("#"+father).removeAttr("checked");
//      Console. Log (" all child checkboxes not selected!" );
  }
 });
};

The above implementation allows one child check box to be selected so that all child check boxes are not selected so that the parent check box is not selected


Related articles: