Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Pasar options de 2 select a un solo select sin que se repitan

Estas en el tema de Pasar options de 2 select a un solo select sin que se repitan en el foro de Javascript en Foros del Web. Hola He probado esto pero solo me pasa las options del select1 al select3. Si uso la segunda funcion me pasa del select2 al select3. ...
  #1 (permalink)  
Antiguo 05/07/2015, 01:01
 
Fecha de Ingreso: mayo-2015
Mensajes: 23
Antigüedad: 8 años, 11 meses
Puntos: 0
Pregunta Pasar options de 2 select a un solo select sin que se repitan

Hola
He probado esto pero solo me pasa las options del select1 al select3.
Si uso la segunda funcion me pasa del select2 al select3.

function boton_onclick() {
select3.options.length = select1.options.length;

for (a = 0; a < select1.options.length; a++) {

select3.options[a].value = select1.options[a].value;
select3.options[a].text = select1.options[a].text;
}
}

function boton_onclick(){
select3.options.length = select2.options.length;

for (b = 0; b < select2.options.length; b++) {

select3.options[b].value = select2.options[b].value;
select3.options[b].text = select2.options[b].text;
}
}

Lo que necesito es pasar las options del select1 y select2 al select3 y que no se repitan, agradeceria mucho la ayuda.
  #2 (permalink)  
Antiguo 05/07/2015, 08:38
 
Fecha de Ingreso: diciembre-2011
Mensajes: 98
Antigüedad: 12 años, 4 meses
Puntos: 29
Respuesta: Pasar options de 2 select a un solo select sin que se repitan

Aquí tienes, primero se pasan todas las "options" de select1 a select3 y luego haces lo mismo con select2 pero con la diferencia que en el segundo bucle recorres también select3 para comprobar que esa "option" en particular no esté ya en select 3:

Código Javascript:
Ver original
  1. function boton_onclick() {
  2.     var select1 = document.getElementById("select1");
  3.     var select2 = document.getElementById("select2");
  4.     var select3 = document.getElementById("select3");
  5.  
  6.     select3.options.length = select2.options.length;
  7.  
  8.     for (i = 0; i < select1.options.length; i++) { //Recorre select1
  9.         select3.options[i].value = select1.options[i].value;
  10.         select3.options[i].text = select1.options[i].text;
  11.     }
  12.  
  13.     for (i = 0; i < select2.options.length; i++) { //Recorre select2
  14.         var added = false;
  15.         for (j = 0; j < select3.options.length; j++) { //Recorre select3 para comprobar que la "option" que vamos a añadir no se encuentre en select3
  16.             if (select2.options[i].value == select3.options[j].value) {
  17.                 if (select2.options[i].text == select3.options[j].text) {
  18.                     added = true;
  19.                 }
  20.             }
  21.         }
  22.  
  23.         if (!added) {
  24.             select3.options.length++;
  25.             select3.options[select3.options.length-1].value = select2.options[i].value;
  26.             select3.options[select3.options.length-1].text = select2.options[i].text;
  27.         }
  28.     }
  29.  
  30. }
  #3 (permalink)  
Antiguo 05/07/2015, 13:06
 
Fecha de Ingreso: mayo-2015
Mensajes: 23
Antigüedad: 8 años, 11 meses
Puntos: 0
Respuesta: Pasar options de 2 select a un solo select sin que se repitan

Lo unico que debo poner la misma cantidad de options en select1 y select 2 si no no funciona
De todas maneras muchas gracias
  #4 (permalink)  
Antiguo 05/07/2015, 13:27
 
Fecha de Ingreso: diciembre-2011
Mensajes: 98
Antigüedad: 12 años, 4 meses
Puntos: 29
Respuesta: Pasar options de 2 select a un solo select sin que se repitan

Prueba con cambiar la linea 6 por:
Código Javascript:
Ver original
  1. select3.options.length = select1.options.length;

No me fije en eso al modificar tu código
  #5 (permalink)  
Antiguo 05/07/2015, 13:48
 
Fecha de Ingreso: mayo-2015
Mensajes: 23
Antigüedad: 8 años, 11 meses
Puntos: 0
Respuesta: Pasar options de 2 select a un solo select sin que se repitan

Listo! amo este foro y sus usuarios *o*

Etiquetas: ayuda!!, javascript+html, select
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:54.