Perl en Español

  1. Home
  2. Tutoriales
  3. Foro
  4. Artículos
  5. Donativos
  6. Publicidad
 

Reiniciar Select

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Javascript
Mensaje Jue Nov 08, 2007 6:37 am
zozo666
Perlero Frecuente
Perlero Frecuente
Registrado: 26 May 2007
Mensajes: 137
Reiniciar Select Responder citando

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

Javascript:
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;
}

y su html es:
HTML:
<body>
<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>
<script language="JavaScript" type="text/JavaScript">
var separator=",";
var h = document.getElementById("dat2").innerHTML;
</script>
<form action="http://www.google.com" method="get" name="hola">
<select name="continente" style="width:150px" onChange="TCN_reload(this);">
</select>
<select name="pais" style="width:150px" onChange="TCN_reload (this);">
  <option selected>pais</option>
</select>
<script language="JavaScript" type="text/JavaScript">
setTimeout("TCN_startCombo(eval(h));",3000);
TCN_makeComboGroup('continente','pais');
</script>
</form>

</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?
Publicar nuevo tema   Responder al tema    Foros de discusión -> Javascript Todas las horas son GMT - 6 Horas
Página 1 de 1



Powered by phpBB © 2001, 2005 phpBB Group