Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/05/2007, 13:20
Avatar de derkenuke
derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 21 años, 3 meses
Puntos: 45
Re: seleccionar grupos de checkbox

Aqui llega la ayuda. He hecho un script que no depende del id del DIV padre, es únicamente DOM:

Código PHP:
<div style="border:solid 1px black; margin:4px;">
    <
p> <input type="checkbox" onchange="cambiaGrupo(this)" />    TODOS NINGUNO</p>
    <
p> <input type="checkbox" /> chk1</p>
    <
p> <input type="checkbox" /> chk2</p>
    <
p> <input type="checkbox" /> chk3</p>
    <
p> <input type="checkbox" /> chk4</p>
</
div>

<
div style="border:solid 1px black; margin:4px;">
    <
p> <input type="checkbox" onchange="cambiaGrupo(this)" />    TODOS NINGUNO</p>
    <
p> <input type="checkbox" /> chk1</p>
    <
p> <input type="checkbox" /> chk2</p>
    <
p> <input type="checkbox" /> chk3</p>
    <
p> <input type="checkbox" /> chk4</p>
    <
p> <input type="checkbox" /> chk5</p>
</
div>

<
div style="border:solid 1px black; margin:4px;">
    <
p> <input type="checkbox" onchange="cambiaGrupo(this)" />    TODOS NINGUNO</p>
    <
p> <input type="checkbox" /> chk1</p>
    <
p> <input type="checkbox" /> chk2</p>
    <
p> <input type="checkbox" /> chk3</p>
</
div>



<
script>

function 
cambiaGrupo(chk) {
    var 
padreDIV=chk;
    while( 
padreDIV.nodeType==&& padreDIV.tagName.toUpperCase()!="DIV" )
        
padreDIV=padreDIV.parentNode;
    
//ahora que padreDIV es el DIV, cogeremos todos sus checkboxes
    
var padreDIVinputs=padreDIV.getElementsByTagName("input");
    for(var 
i=0i<padreDIVinputs.lengthi++) {
        if( 
padreDIVinputs[i].getAttribute("type")=="checkbox" )
            
padreDIVinputs[i].checked chk.checked;
    }
}

</script> 
Te va a venir bien, supongo. Con el while vamos mirando cada padre, a ver si tiene de etiqueta DIV, y cuando la tenga paramos, así ya tenemos el DIV y sólo queda recoger los checkboxes hijos.


Un saludo.
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.