Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/02/2010, 14:59
ejfr1985
 
Fecha de Ingreso: febrero-2010
Mensajes: 1
Antigüedad: 14 años, 3 meses
Puntos: 0
seleccionar checkbox padre al seleccionar hijo

hola saludos, he estado realizando una funcion que permita seleccionar un grupo de checkbox al seleccionar el padre, [URL="http://www.forosdelweb.com/f13/seleccionar-grupos-checkbox-490465/"]http://www.forosdelweb.com/f13/seleccionar-grupos-checkbox-490465/[/URL] en este enlace encontre gran parte de la solucion mi problema es que no puedo seleccionar el padre de los checkboxes al seleccionar el hijo solo funciona de padre a hijo

este es el codigo que tengo hasta el momento
Código PHP:
<script type="text/javascript"> 
function cambiaGrupo(chk) {
    var padreDIV=chk;
    while( padreDIV.nodeType==1 && 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=0; i<padreDIVinputs.length; i++) {
        if( padreDIVinputs[i].getAttribute("type")=="checkbox" )
            padreDIVinputs[i].checked = chk.checked;
    }
}

function marcaPadre(chk){
     var padreFIELDSET=chk;
    while( padreFIELDSET.nodeType==1 && padreFIELDSET.tagName.toUpperCase()!="DIV" )
        padreFIELDSET=padreFIELDSET.parentNode;
    var padreFIELDSEThijos=padreFIELDSET.childNodes;
    for(var i=0, chk, todosChecked=true; i<padreFIELDSEThijos.length && todosChecked==false; i++) {
        if( padreFIELDSEThijos[i].nodeType == 1                                // Tiene que ser un nodo tipo HTML, no texto
             &&            // No puede estar dentro de un LEGEND 
                                      // Tiene que tener hijos (el checkbox)
            (chk=padreFIELDSEThijos[i].getElementsByTagName("input")[0]) &&        // Seleccionamos al hijo input como chk
            chk.getAttribute("type")=="checkbox" &&                                // chk tiene que ser efectivamente un checkbox
            chk.checked==true                                                    // y tiene que estar no seleccionado
            ) {
            todosChecked = false;
        }
    }
    // Si están todos checked tenemos que activar el primer checkbox hijo
    // Si no están todos checked tenemos que desactivar el primer checkbox hijo
    padreFIELDSET.getElementsByTagName("input")[0].checked = todosChecked 
    } 
</script>
<?php
function drawTree($root,&$level, & $sm, & $checked$parentId=null) {
    
    
$children $sm->getSubOpcion($root['id_opcion']);
    
    
$chk '';
    
//echo '<li> buscando ', $root['id_opcion'], ' en ', print_r($checked,1), ' econtrado : ' ,var_dump(array_search($root['id_opcion'],$checked,true)),'</li>';
    
if(array_search($root['id_opcion'],$checked,true) !== false) {
        
$chk 'checked="checked"' ;
    }
    
    echo 
'<div> <li>';     
?>
        <img src="imagenes/mas.gif" onclick="$(this).toggle(); $(this).next('img').toggle()<?php if (count($children) > 0){ ?>;showSubMenu('menu_<?php echo $root['id_opcion'?>')<?php ?>" style="cursor:pointer<?php if (count($children) < 1){ ?>; visibility:hidden <?php ?>" />
        <img src="imagenes/menos.gif" onclick="$(this).toggle(); $(this).prev('img').toggle()<?php if (count($children) > 0){ ?>;showSubMenu('menu_<?php echo $root['id_opcion'?>')<?php ?>" style="display:none; cursor:pointer" />
<?php  ?> 
    <input name="opciones[' <?php echo $root['id_opcion'];  ?>']" id="opciones_<?php echo $root['id_opcion'];?>" type="checkbox" value="<?php echo $root['id_opcion']; ?>"   <?php echo $chk?>   onchange="cambiaGrupo(this);marcaPadre(this);" /> <span title="'<?php echo $root['descripcion']; ?>'";
    
    <?php /*if (count($children) > 0) {
        echo 'onclick="showSubMenu(\'menu_' . $root['id_opcion'] . '\')" style="cursor:pointer"';
    }*/
?>
    
     > <?php echo $level ' ' htmlentities($root['titulo'],null,'utf-8') , '</span>';
    
    if (
count($children) > 0) {
        echo 
'<ul style="display:none;" id="menu_' $root['id_opcion'] . '">';
        foreach (
$children as $child) {
            
            
drawTree($child,++$level,$sm,$checked$root['id_opcion']);
            
        }
        echo 
'</ul>'
    }
    
    echo 
'</li></div>';
    --
$level;
    
}

?>
como ven cada hijo es un padre a la vez y no se como aplicar el concepto de la funcion a el padre que lo contiene, si me pueden ayudar muy agradecido estare