Ver Mensaje Individual
  #4 (permalink)  
Antiguo 07/08/2013, 02:30
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 2 meses
Puntos: 574
Respuesta: Select que va a otro select

Ahora funciona

Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <title>Documento sin t&iacute;tulo</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. </head>
  5. </body>
  6. <script language="JavaScript" type="text/JavaScript">
  7. var s1 = ['seleccione','1','2','3'];
  8. var s2 = ['11','12','13'];
  9. var s3 = ['21','22','23'];
  10. var s4 = ['31','32','33'];
  11. var select1 = document.createElement('select');
  12. var select2 = document.createElement('select');
  13. //Inicializació
  14. var contaglobal = new Array();
  15. //Agregamos un identificador
  16. select1.setAttribute("id","select1");
  17. select2.setAttribute("id","select2");
  18. document.body.appendChild(select1);
  19. document.body.appendChild(select2);
  20. for ( x = 0 ; x < s1.length ; x++) {
  21.     var option = document.createElement('option');
  22.     option.innerHTML = s1[x];
  23.     select1.appendChild(option);
  24.     }
  25. select1.onchange = function () {
  26.     //Reiniciación
  27.     contaglobal = new Array();
  28.     var conta = select1.value;
  29.     if ( conta == "1"){
  30.         contaglobal = s2 ;
  31.     } else if ( conta == "2") {
  32.         contaglobal = s3;
  33.     } else {
  34.         contaglobal = s4;
  35.     }
  36.     //Eliminamos las opciones anteriores de select 2
  37.     for( var o = 0; o < document.getElementById("select2").options.length; o++ ){
  38.         document.getElementById("select2").remove(0);//Siempre la primera
  39.     }
  40.     //Creamos las nuevas
  41.     for( var o = 0; o < contaglobal.length; o++ ){
  42.         var opcion = document.createElement('option');
  43.         opcion.innerHTML = contaglobal[o];
  44.         select2.appendChild(opcion);
  45.     }
  46. }</script>
  47. </html>
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.