How to check the father, when I select the child in a checkbox with jQuery

7

How could I do a checked event in jQuery? I have this structure:

With the serious code:

<ul>
    <li class="padre_menu">
        <div class="checkbox">
            <label>
                <input name="padre[]" type="checkbox" class="ace ace-checkbox-2" id="id_padre" value="1">
                <span class="lbl"> Inicio</span>
            </label>
        </div>
    </li>
    <li class="padre_menu">
        <div class="checkbox">
            <label>
                <input name="padre[]" type="checkbox" class="ace ace-checkbox-2" id="id_padre" value="2">
                <span class="lbl"> Mail</span>
            </label>
        </div>
        <ul>
            <li class="hijo_menu">
                <div class="checkbox">
                    <label>
                        <input name="hijo[]" type="checkbox" id="id_hijo" class="ace" value="5">
                        <span class="lbl"> Sub Mail</span>
                    </label>
                </div>
            </li>
            <li class="hijo_menu">
                <div class="checkbox">
                    <label>
                        <input name="hijo[]" type="checkbox" id="id_hijo" class="ace" value="6">
                        <span class="lbl"> test1</span>
                    </label>
                </div>
            </li>
        </ul>
    </li>
</ul>

What I need is that when I select "Sub Mail" or "test1" I also select the "Mail", or if I mark only "Mail", I select the 2 items that it brings.

    
asked by Jean Paul 07.09.2016 в 17:02
source

2 answers

5

This jQuery-based solution consists of:

  • When changing the state of a "father", we go up a bit in the DOM to its container <li> , then find the next <ul> that contains the respective children, and apply the same effect ( check / uncheck) to the checkbox elements.
  • When changing the status of one of the "children", if we do a check , we simply do a similar process to the previous one, but in reverse to do the respective "father" check.
  • If we did uncheck of one of the "children", then we go to our <ul> container of "children" and see if there is at least one "son" checkbox selected or not. In case there is one selected, we do not do anything and the checkbox "father" is still selected, otherwise we remove the check from the "father".

// cuando cambia el valor del padre hacemos lo mismo con sus hijos
$('.padre_menu input[type=checkbox]').change(function() {
  $(this).closest('.padre_menu').next('ul').find('.hijo_menu input[type=checkbox]').prop('checked', this.checked);  
});


// cuando cambia el valor de un hijo determinamos si el padre debe ser 'chequeado' o no
$('.hijo_menu input[type=checkbox]').change(function() {
  var closestUl = $(this).closest('ul');
  var checkedParent = true;
  if(closestUl.find('input[type=checkbox]:checked').length == 0) {
    checkedParent = false;
  }
  
  closestUl.prev('.padre_menu').find('input[type=checkbox]').prop('checked', checkedParent);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<ul>
    <li class="padre_menu">
        <div class="checkbox">
            <label>
                <input name="padre[]" type="checkbox" class="ace ace-checkbox-2" id="id_padre" value="1">
                <span class="lbl"> Inicio</span>
            </label>
        </div>
    </li>
    <li class="padre_menu">
        <div class="checkbox">
            <label>
                <input name="padre[]" type="checkbox" class="ace ace-checkbox-2" id="id_padre" value="2">
                <span class="lbl"> Mail</span>
            </label>
        </div>
    </li>
    <ul>
        <li class="hijo_menu">
            <div class="checkbox">
                <label>
                    <input name="hijo[]" type="checkbox" id="id_hijo" class="ace" value="5">
                    <span class="lbl"> Sub Mail</span>
                </label>
            </div>
        </li>
        <li class="hijo_menu">
            <div class="checkbox">
            <label>
                <input name="hijo[]" type="checkbox" id="id_hijo" class="ace" value="6">
                <span class="lbl"> test1</span>
            </label>
            </div>
        </li>
    </ul>

</ul>
    
answered by 07.09.2016 / 17:50
source
0

The following example complies with recursion, something necessary when working with nesting

$(function () {
 $(".autoCheckbox").on("click",function () {
  var expr="li input[type=checkbox]",$this=$(event.target);
  while ($this.length) {
   $input=$this.closest("li").find(expr);
   if ($input.length) {
    if ($this[0]==event.target) {
     checked = $this.prop("checked");
     $input.prop("checked", checked).css("opacity","1.0");
    }
    checked=$input.is(":checked");
    $this.prop("checked", checked).css("opacity",
     (checked && $input.length!= $this.closest("li").find(expr+":checked").length)
      ? "0.5" : "1.0");
   }
   $this=$this.closest("ul").closest("li").find(expr.substr(3)+":first");
  }
 });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="autoCheckbox">
  <li>
   <input type="checkbox" id="Inicio">Inicio
   <ul>
    <li>
     <input type="checkbox" id="Sub-Mail">Sub-Mail
    </li>
    <li>
     <input type="checkbox" id="test">test
     <ul>
      <li>
       <input type="checkbox" id="Sub-Mail 2">Sub-Mail 2
      </li>
      <li>
       <input type="checkbox" id="test 2">test 2
        <ul>
         <li>
          <input type="checkbox" id="Sub-Mail 3">Sub-Mail 3
         </li>
         <li>
          <input type="checkbox" id="test 3">test 3
         </li>
        </ul>
      </li>
     </ul>
    </li>
   </ul>
  </li>
  <li>
   <input type="checkbox" id="Mail">Mail
   <ul>
    <li>
     <input type="checkbox" id="Sub-Mail">Sub-Mail
    </li>
    <li>
     <input type="checkbox" id="test">test
     <ul>
      <li>
       <input type="checkbox" id="Sub-Mail 2">Sub-Mail 2
        <ul>
         <li>
          <input type="checkbox" id="Sub-Mail 3">Sub-Mail 3
         </li>
         <li>
          <input type="checkbox" id="test 3">test 3
         </li>
        </ul>
      </li>
      <li>
       <input type="checkbox" id="test 2">test 2
        <ul>
         <li>
          <input type="checkbox" id="Sub-Mail 3">Sub-Mail 3
         </li>
         <li>
          <input type="checkbox" id="test 3">test 3
         </li>
        </ul>
      </li>
     </ul>
    </li>
   </ul>
  </li>
 </ul>
    
answered by 12.04.2017 в 02:37