Select with redirect

4

I do not know how to program! but I can make small changes. I need to create a select and when the person chooses the item, it will be redirected

<select>
  <option value="Vilhena">Vilhena</option>
  <option value="Cacoal">Cacoal</option>
  <option value="Cerejeiras">Cerejeiras</option>
</select>

Then when selecting Vilhena, be redirected to another page.

    
asked by anonymous 24.07.2015 / 19:16

4 answers

3

Hello, you can do as follows: Make a javascript.

JavaScript

<script>
            function AbrirSecao(secao){
                window.open(""+secao+"", "_parent");
            }
</script>

HTML

<select name="unidade" id="unidade" onChange="AbrirSecao(this.value)">
    <option value="">Selecione sua Cidade</option>
    <option value="http://www.google.com.br">Sua Cidade 1</option>
    <option value="http://www.google.com.br">Sua Cidade 2</option>
</select>

With this, when you select the city, it will be redirected automatically by JavaScript.

    
24.07.2015 / 19:22
3

Or if you want to use jQuery:

<select id="selecao">
    <option value="http://www.exemplo.com.br">Opção 1</option>
    <option value="http://www.exemplo.com.br">Opção 2</option>
    <option value="http://www.exemplo.com.br">Opção 3</option>
</select>

// requer jquery
<script>
$('#selecao').change(function() {
    window.location = $(this).val();
});
</script>
    
24.07.2015 / 19:29
1

You can also simply put a onclick="location.href='seusite'" in the event onclick of the element option

<select>
  <option value="Vilhena" onclick="location.href='seusite'">Vilhena</option>
  <option value="Cacoal" onclick="location.href='seusite'">Cacoal</option>
  <option value="Cerejeiras" onclick="location.href='seusite'">Cerejeiras</option>
</select>
    
24.07.2015 / 19:23
0

You can do this using native JavaScript. That is, without any external library, and without mixing JavaScript in HTML ( which is not very advisable ).

In this case you need to consider 3 things:

  • What to put in HTML
  • the event sink
  • where to put JavaScript

In HTML the visible part is what is between > and < of each option . What's in value is what's important to the programming part. So instead of having the same in value and the text of option puts value the URL you want. For example:

<option value="/Vilhena.html">Vilhena</option>

To select the element you can use:

var select = document.querySelector('select');

This will work fine if you only have 1 select on the page. If you have more than one you must give an ID to the select (the HTML would be: <select id="meuSelect"> ) and you can select it like this:

var select = document.getElementById('meuSelect');
// ou assim:
var select = document.querySelector('#meuSelect');

Then add an event changer to it when you change:

select.addEventListener('change', function(){
    var valor = this.value;
    window.location.href = valor;
});

Finally puts the script at the bottom of the page, before the </body> tag. This way JavaScript is read with the HTML already loaded into memory.

    
25.07.2015 / 20:56