• Publicidad

Reiniciar Select

Discute todo acerca de JavaScript así como DHTML o la tecnología AJAX.

Reiniciar Select

Notapor zozo666 » 2007-11-08 07:37 @359

Les cuento. Tengo un script de JavaScript para hacer Select dependientes y su código es este:

Sintáxis: [ Descargar ] [ Ocultar ]
Using javascript Syntax Highlighting
contents=new Array();

function TCN_addContent(str){
        contents.push(str);
        arrayValues = new Array();
        for(i=0;i<contents.length;i++){
                arrayValues[i]=contents[i].split(separator);
        }
}

function TCN_makeComboGroup(){
        comboGroup=new Array();
        args=TCN_makeComboGroup.arguments;
        for(i=0;i<args.length;i++){
                comboGroup[i]=MM_findObj(args[i]);
        }
}

function TCN_startCombo(){
        combo1=comboGroup[0];
        for (i=0;i<arrayValues.length;i++){
                existe=false;
                for(j=0;j<combo1.options.length;j++){
                        if(arrayValues[i][0]==combo1.options[j].text){
                                existe=true;
                        }
                }
                if(existe==false){
                        combo1.options[combo1.options.length]=new Option(arrayValues[i][0],arrayValues[i][1]);
                }//end if
        }//for(i)
        combo1.options[0].selected=true;
        TCN_reload(combo1);
}//function

function TCN_reload(from){
//averiguamos el indice del combo que llama:
        for(j=0;j<comboGroup.length;j++){
                if(comboGroup[j]==from){
                        //el nuestro es el siguiente
                        i=j+1;
                        thisCombo=comboGroup[i];
                        prevCombo=comboGroup[i-1];
                        prevComboTextIndex=j*2;
                        thisComboIndex=i;
                        thisComboTextIndex=(i*2);
                        thisComboValueIndex=(i*2)+1;
                }
        }
        for (m=thisCombo.options.length-1;m>=0;m--){
                thisCombo.options[m]=null;
        }
        for(i=0;i<arrayValues.length;i++){
                existe=false;
                if(arrayValues[i][prevComboTextIndex]==prevCombo.options[prevCombo.selectedIndex].text){
                        for(j=0;j<thisCombo.options.length;j++){
                                if(arrayValues[i][thisComboTextIndex]==thisCombo.options[j].text){
                                        existe=true;
                                }
                        }
                        if(existe==false){
                                thisCombo.options[thisCombo.options.length]=new Option(arrayValues[i][thisComboTextIndex],arrayValues[i][thisComboValueIndex]);
                        }
                }
        }
        thisCombo.options[0].selected=true;
        if(thisComboIndex<comboGroup.length-1){
                TCN_reload(thisCombo);
        }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

y su html es:
Sintáxis: [ Descargar ] [ Ocultar ]
  1. <body> 
  2. <div id="dat2" style="display:none">TCN_addContent("Sudafrica,1,EE.UU,1");TCN_addContent("America,2,Argentina,2");TCN_addContent("Europa,3,R.Checa,1");TCN_addContent("Europa,3,Inglaterra,2");TCN_addContent("Europa,3,Yugolasvia,3");</div> 
  3. <script language="JavaScript" type="text/JavaScript"> 
  4. var separator=","; 
  5. var h = document.getElementById("dat2").innerHTML; 
  6. </script> 
  7. <form action="http://www.google.com" method="get" name="hola"> 
  8. <select name="continente" style="width:150px" onChange="TCN_reload(this);"> 
  9. </select> 
  10. <select name="pais" style="width:150px" onChange="TCN_reload (this);"> 
  11. <option selected>pais</option> 
  12. </select> 
  13. <script language="JavaScript" type="text/JavaScript"> 
  14. setTimeout("TCN_startCombo(eval(h));",3000); 
  15. TCN_makeComboGroup('continente','pais'); 
  16. </script> 
  17. </form> 
  18.  
  19. </body> 


Bueno como se ve, lo que hago es tomar el innerHTML del div y cargárselo a "h" para después ejecutar TCN_startCombo(eval(h));. Bueno esto lo arme todo con un CGI::Ajax y funciona a la perfección. El tema es que necesitaría una forma de poder borrar todos los select completamente, porque cuando borro algún dato de la base de datos, no lo borra, y si lo modifico, me agrega una igual con la modificación. ¿Hay algo en el código el cual tenga que ponerlo en 0 para que me lo agregue perfectamente sin problemas?
zozo666
Perlero nuevo
Perlero nuevo
 
Mensajes: 139
Registrado: 2007-05-26 10:36 @483

Publicidad

Volver a JavaScript

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 2 invitados

cron