Ver Mensaje Individual
  #11 (permalink)  
Antiguo 13/07/2012, 13:08
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: link dependiente de un select

Cita:
Iniciado por racba Ver Mensaje
Hola a todos:
Que les parece este codigo:
Código HTML:
Ver original
  1. <script type="text/javascript">
  2.  function cambiar_link(){
  3.       var _link = new Array('milink1', 'milink2', 'milink3');
  4.       alert('Este es el array con todos los links: '+_link);
  5.       _div = document.getElementById('id_div');
  6.       _select = document.getElementById('id_select')
  7.       _optioncant = _select.length;
  8.       alert ('Las cantidad de opciones son: '+_optioncant);
  9.       _option=_select.options[_select.selectedIndex].index;
  10.       alert('Esta es la opcion numero: '+_option+ '. Recordar que la primera opcion es 0');
  11.       for (i=1; i< _optioncant; i++){
  12.           if (i == _option){
  13.            _div.innerHTML = '<a href="http://www.'+_link[i-1]+'.com">Mi link '+i+'</a>';
  14.             break;
  15.           }
  16.        }
  17.     }
  18.    </script>
  19. </head>
  20.   <select id="id_select" onchange="cambiar_link()">
  21.    <option>OPCIONES</option>
  22.    <option>opcion 1</option>
  23.    <option>opcion 2</option>
  24.    <option>opcion 3</option>
  25.   </select>
  26.   </form>
  27.   <div id='id_div'></div>
  28.   </body>
  29.  </html>
Haya paz !!!

@racba
permitime simplificarlo un poco

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  5. <title>cambiar href</title>
  6. <script type="text/javascript">
  7. //<![CDATA[
  8.    function cambiar_link(){
  9.    var elLink = document.getElementById('id_select');
  10.    if(elLink.value !== ""){
  11.    document.getElementById('destino').href = elLink.value;
  12.    document.getElementById('destino').innerHTML =elLink.options[elLink.selectedIndex].text;
  13.    }
  14.    }
  15. //]]>
  16. </head>
  17. <form action="#">
  18. <select id="id_select" onchange="cambiar_link()">
  19. <option value="">OPCIONES</option>
  20. <option value="http://google.com">google</option>
  21. <option value="http://forosdelweb.com">foros del web</option>
  22. <option value="http://emprear.com">emprear</option>
  23. </form>
  24. <a href="#" id="destino">ir a</a>
  25. </body>
  26. </html>

De todas maneras, mucho sentido no le encuentro (a lo mejor viendo todo el conjunto...)

Las opciones más comunes para esto serían

A. que al hacer onchange() haga directamente la redirección
B. no hacer el onchange(), agregar un botón y que ejecute la función onclick()
@carlod_39
sin ánimo de inmiscuirme en tu pequeño entredicho con @furoya, si hubieses comentado
Cita:
efectivamente lo que quería era que al seleccionar una opción en el select la dirección del link cambia el href
oportunamente, este posts no tendría más de 3 respuestas (incluido tu agradecimiento)

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.