Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/03/2011, 09:56
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Multiples selects dinámicos

Saludos corillo,

Estoy trabajando con una aplicación que requiere de multiples selects y que todos se formen de acuerdo a un valor seleccionado. Lo logré pero es un poco complicado trabajarlo. Esta es la forma como lo hago al momento.

Nota: Estos los formo con PHP por lo que se me hace menos difícil formarlos
Código HTML:
Ver original
  1. <script type="text/javascript">
  2. function nullOptions(obj){
  3.     var n=obj.options.length
  4.     for (i=0;i<n;i++){
  5.         obj.options[i]=null
  6.     }
  7.     obj.options.length=0;
  8. }
  9.  
  10.  
  11. /**
  12. * Seconds elements
  13. */
  14. function secondElements1(obj){
  15.     nullOptions(obj);
  16.     var n=0;
  17.     with(obj){
  18.         options[n++]=new Option("Selecciona bar","");
  19.         options[n++]=new Option(20, 20);
  20.         options[n++]=new Option(30, 30);
  21.         options[n++]=new Option(40, 40);
  22.     }
  23. }
  24. function secondElements2(obj){
  25.     nullOptions(obj);
  26.     var n=0;
  27.     with(obj){
  28.         options[n++]=new Option("Selecciona bar","");
  29.         options[n++]=new Option(200, 200);
  30.         options[n++]=new Option(300, 300);
  31.         options[n++]=new Option(400, 400);
  32.     }
  33. }
  34. function secondElements3(obj){
  35.     nullOptions(obj);
  36.     var n=0;
  37.     with(obj){
  38.         options[n++]=new Option("Selecciona bar","");
  39.         options[n++]=new Option(2000, 2000);
  40.         options[n++]=new Option(3000, 3000);
  41.         options[n++]=new Option(4000, 4000);
  42.     }
  43. }
  44.  
  45. /**
  46. * Thirds elements
  47. */
  48.  
  49. /**
  50. * Elementos para el valor 20, 30 y 40 de bar
  51. */
  52. function thirdElements1000(obj){
  53.     nullOptions(obj);
  54.     var n=0;
  55.     with(obj){
  56.         options[n++]=new Option("Selecciona candy","");
  57.         options[n++]=new Option(21, 21);
  58.         options[n++]=new Option(22, 22);
  59.         options[n++]=new Option(23, 23);
  60.     }
  61. }
  62. function thirdElements1001(obj){
  63.     nullOptions(obj);
  64.     var n=0;
  65.     with(obj){
  66.         options[n++]=new Option("Selecciona candy","");
  67.         options[n++]=new Option(31, 31);
  68.         options[n++]=new Option(32, 32);
  69.         options[n++]=new Option(33, 33);
  70.     }
  71. }
  72. function thirdElements1002(obj){
  73.     nullOptions(obj);
  74.     var n=0;
  75.     with(obj){
  76.         options[n++]=new Option("Selecciona candy","");
  77.         options[n++]=new Option(41, 41);
  78.         options[n++]=new Option(42, 42);
  79.         options[n++]=new Option(43, 43);
  80.     }
  81. }
  82.  
  83. /**
  84. * Elementos para los valores 200, 300 y 400 de bar
  85. */
  86. function thirdElements1003(obj){
  87.     nullOptions(obj);
  88.     var n=0;
  89.     with(obj){
  90.         options[n++]=new Option("Selecciona candy","");
  91.         options[n++]=new Option(210, 210);
  92.         options[n++]=new Option(220, 220);
  93.         options[n++]=new Option(230, 230);
  94.     }
  95. }
  96. function thirdElements1004(obj){
  97.     nullOptions(obj);
  98.     var n=0;
  99.     with(obj){
  100.         options[n++]=new Option("Selecciona candy","");
  101.         options[n++]=new Option(310, 310);
  102.         options[n++]=new Option(320, 320);
  103.         options[n++]=new Option(330, 330);
  104.     }
  105. }
  106. function thirdElements1005(obj){
  107.     nullOptions(obj);
  108.     var n=0;
  109.     with(obj){
  110.         options[n++]=new Option("Selecciona candy","");
  111.         options[n++]=new Option(410, 410);
  112.         options[n++]=new Option(420, 420);
  113.         options[n++]=new Option(430, 430);
  114.     }
  115. }
  116.  
  117. /**
  118. * Elementos para los valores 2000, 3000 y 4000 de bar
  119. */
  120. function thirdElements1006(obj){
  121.     nullOptions(obj);
  122.     var n=0;
  123.     with(obj){
  124.         options[n++]=new Option("Selecciona candy","");
  125.         options[n++]=new Option(2100, 2100);
  126.         options[n++]=new Option(2200, 2200);
  127.         options[n++]=new Option(2300, 2300);
  128.     }
  129. }
  130. function thirdElements1007(obj){
  131.     nullOptions(obj);
  132.     var n=0;
  133.     with(obj){
  134.         options[n++]=new Option("Selecciona candy","");
  135.         options[n++]=new Option(3100, 3100);
  136.         options[n++]=new Option(3200, 3200);
  137.         options[n++]=new Option(3300, 3300);
  138.     }
  139. }
  140. function thirdElements1008(obj){
  141.     nullOptions(obj);
  142.     var n=0;
  143.     with(obj){
  144.         options[n++]=new Option("Selecciona candy","");
  145.         options[n++]=new Option(4100, 4100);
  146.         options[n++]=new Option(4200, 4200);
  147.         options[n++]=new Option(4300, 4300);
  148.     }
  149. }
  150.  
  151. function firstFunction(obj){
  152.     var firstElement = obj.first;
  153.     var secondElement = obj.second;
  154.     var thirdElement = obj.third;
  155.     switch(firstElement.selectedIndex){
  156.         case 0:
  157.             secondElement.options.length=0;
  158.             secondElement.options[0]=new Option("[Selecciona foo]","");
  159.             secondElement.options[0].selected=true;
  160.  
  161.             thirdElement.options.length=0;
  162.             thirdElement.options[0]=new Option("[Selecciona foo]","");
  163.             thirdElement.options[0].selected=true;
  164.             break;
  165.         case 1:
  166.             secondElements1(secondElement);
  167.             secondElement.options[0].text="[Selecciona bar]";
  168.  
  169.             thirdElement.options.length=0;
  170.             thirdElement.options[0]=new Option("[Selecciona bar]","");
  171.             thirdElement.options[0].selected=true;
  172.             break;
  173.         case 2:
  174.             secondElements2(secondElement);
  175.             secondElement.options[0].text="[Selecciona bar]";
  176.  
  177.             thirdElement.options.length=0;
  178.             thirdElement.options[0]=new Option("[Selecciona bar]","");
  179.             thirdElement.options[0].selected=true;
  180.             break;
  181.         case 3:
  182.             secondElements3(secondElement);
  183.             secondElement.options[0].text="[Selecciona bar]";
  184.  
  185.             thirdElement.options.length=0;
  186.             thirdElement.options[0]=new Option("[Selecciona bar]","");
  187.             thirdElement.options[0].selected=true;
  188.             break;
  189.     }
  190. }
  191.  
  192. function secondFunction(obj){
  193.     var firstElement = obj.first;
  194.     var secondElement = obj.second;
  195.     var thirdElement = obj.third;
  196.     switch(firstElement.selectedIndex){
  197.         case 0:
  198.             secondElement.options.length=0;
  199.             secondElement.options[0]=new Option("[Selecciona foo]","");
  200.             secondElement.options[0].selected=true;
  201.  
  202.             thirdElement.options.length=0;
  203.             thirdElement.options[0]=new Option("[Selecciona foo]","");
  204.             thirdElement.options[0].selected=true;
  205.             break;
  206.         case 1:
  207.             switch(secondElement.selectedIndex){
  208.                 case 0:
  209.                     thirdElement.options.length=0;
  210.                     thirdElement.options[0]=new Option("[Selecciona bar]","");
  211.                     thirdElement.options[0].selected=true;
  212.                     break;
  213.                 case 1:
  214.                     thirdElements1000(thirdElement);
  215.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  216.                     thirdElement.options[0].selected=true;
  217.                     break;
  218.                 case 2:
  219.                     thirdElements1001(thirdElement);
  220.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  221.                     thirdElement.options[0].selected=true;
  222.                     break;
  223.                 case 3:
  224.                     thirdElements1002(thirdElement);
  225.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  226.                     thirdElement.options[0].selected=true;
  227.                     break;
  228.             }
  229.             break;
  230.         case 2:
  231.             switch(secondElement.selectedIndex){
  232.                 case 0:
  233.                     thirdElement.options.length=0;
  234.                     thirdElement.options[0]=new Option("[Selecciona bar]","");
  235.                     thirdElement.options[0].selected=true;
  236.                     break;
  237.                 case 1:
  238.                     thirdElements1003(thirdElement);
  239.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  240.                     thirdElement.options[0].selected=true;
  241.                     break;
  242.                 case 2:
  243.                     thirdElements1004(thirdElement);
  244.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  245.                     thirdElement.options[0].selected=true;
  246.                     break;
  247.                 case 3:
  248.                     thirdElements1005(thirdElement);
  249.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  250.                     thirdElement.options[0].selected=true;
  251.                     break;
  252.             }
  253.             break;
  254.         case 3:
  255.             switch(secondElement.selectedIndex){
  256.                 case 0:
  257.                     thirdElement.options.length=0;
  258.                     thirdElement.options[0]=new Option("[Selecciona bar]","");
  259.                     thirdElement.options[0].selected=true;
  260.                     break;
  261.                 case 1:
  262.                     thirdElements1006(thirdElement);
  263.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  264.                     thirdElement.options[0].selected=true;
  265.                     break;
  266.                 case 2:
  267.                     thirdElements1007(thirdElement);
  268.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  269.                     thirdElement.options[0].selected=true;
  270.                     break;
  271.                 case 3:
  272.                     thirdElements1008(thirdElement);
  273.                     thirdElement.options[0]=new Option("[Selecciona candy]","");
  274.                     thirdElement.options[0].selected=true;
  275.                     break;
  276.             }
  277.             break;
  278.     }
  279. }
  280. <form action="#">
  281.     Foo:
  282.     <select name="first" onchange="firstFunction(this.form)">
  283.         <option value="">[Selecciona uno]</option>
  284.         <option value="1">1</option>
  285.         <option value="100">100</option>
  286.         <option value="1000">1000</option>
  287.     </select><br />
  288.  
  289.     Bar:
  290.     <select name="second" onchange="secondFunction(this.form)">
  291.         <option value="">[Selecciona Foo]</option>
  292.     </select><br />
  293.  
  294.     Candy:
  295.     <select name="third">
  296.         <option value="">[Selecciona Foo]</option>
  297.     </select>
  298. </form>

Lo que me interesa saber es alguna otra forma de lograrlo pero sin un framework y sin ajax que también creo que es otra forma sencilla. Lo dejo por si le ayuda a alguien, aunque está demasiado grande el código para lograr lo que yo quiero. La razón por la pregunta es que luego voy a tener que trabajar que el último valor modifique otro valor y va a ser un código muy grande para trabajar. Bueno me dejan saber

¡Muchas gracias!
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos