2008-10-01 8 views
2

Pouvez-vous faire un meilleur code? Je dois cocher/décocher tous les enfants en fonction du parent et quand un enfant est coché, cocher parent, quand tous les enfants sont décochés, décochez parent.Cocher/décocher les parents et les enfants

$(".parent").children("input").click(function() { 
    $(this).parent().siblings("input").attr("checked", this.checked); 
}); 

$(".parent").siblings("input").click(function() { 
    if (this.checked) { 
     $(this).siblings("div").children("input").attr("checked", true); 
     return; 
    } 

    var childs = $(this).siblings("div").siblings("input"); 

    for (i = 0; i < childs.length; i++) { 
     if ($(childs.get(i)).attr("checked")) 
      return; 
    } 

    $(this).parent().children("div").children("input").attr("checked", false); 
}); 

Répondre

1
$(".parent").children("input").click(function() { 
    $(this).parent().siblings("input").attr("checked", this.checked); 
}); 

$(".parent").siblings("input").click(function() { 
    $(this).siblings("div").children("input").attr("checked", 
     this.checked || $(this).siblings("input[checked]").length>0 
    ); 
}); 
+0

quel est le but de faire ".siblings (...). Siblings (...)"? Aussi: "$ (this) .parent(). Children (...)" pourrait être ".siblings (...)" – nickf

+0

nickf: guh, pour voir si vous faisiez attention :) - en fait, je viens copié à partir du code de Zote (ligne var childs) - je l'ai nettoyé plus (+1) – Hafthor

1

Woah, je suis méga confus. il semble que vous avez des entrées avec d'autres entrées à l'intérieur d'eux? ... ce qui n'a pas de sens. Voici ce que je pense votre structure ressemble, alors je vais ici.

<div class="parent"> 
    <input type="checkbox" /> 
    <div> 
     <input type="checkbox" /> 
     <input type="checkbox" /> 
    </div> 
    <input type="checkbox" /> 
    <div> 
     <input type="checkbox" /> 
     <input type="checkbox" /> 
    </div> 
</div> 

Et voici le code que j'utiliserais.

$("input[type='checkbox']").click(function() { 
    // turn on or off all descendants. 
    $(this)   // get this checkbox 
     // get the div directly after it 
     .next('div') 
     // get ALL the inputs in that div (not just first level children) 
     .find("input[type='checkbox']") 
     .attr("checked", this.checked) 
    ; 

    // now check if we have to turn the parent on or off. 
    $(this) 
     .parent() // this will be the div 
     .prev('input[type="checkbox"]') // this is the input 
     .attr(
      "checked",  // set checked to true if... 
      this.checked // this one is checked, or... 
      || $(this).siblings("input[type='checkbox'][checked]").length > 0 
          // any of the siblings are checked. 
     ) 
    ; 
}); 

mise à jour: je viens de tester cela et ça marche totalement (woo!). Il fonctionne également avec autant de niveaux d'imbrication que vous le souhaitez, et pas seulement deux.

0

Ceci est un joli petit fil qui m'a presque où je dois être. J'ai légèrement adapté le code de Nickf. Le but est que la vérification d'un enfant vérifie également le parent, et décochez un parent décocherait également tous les enfants.

$ ("input [type = 'checkbox']") cliquez sur (function() { if ($ (this.checked)) {

$(this) 
    .next('div') 
    .find("input[type='checkbox']") 
    .attr("checked", this.checked) 
; 
} 


$(this) 
    .parent() 
    .prev('input[type="checkbox"]') 
    .attr("checked", this.checked || $(this).siblings("input[type='checkbox'][checked]").length > 0 

    ) 
; 

}!).

Comment est-ce que l'on pourrait modifier cette option pour que tous les descendants ne soient pas cochés si le parent n'est pas coché?

Nouveau toutes les choses JQ et javascript..apologies.

0
$('.parent').click(function(){ 
    checkBox = $(this).find('input[type=checkbox]'); 
    checkBox.prop("checked", !checkBox.prop("checked")); // inverse selection 
}); 

Ultime.

Questions connexes