-2

now i have the following code:

<form class="demo">
<select>
    <option selected>Selecione sua cidade</option>
    <option value='JP'>João Pessoa - PB</option>
    <option value='outros'>Outros</option>
</select>
</form>

What i need:

When visitor click on dropdown menu and select a option , they go to http://www.example.com Each option will go to different adress.

Value='JP' goes to example.com

Value='outros' goes to example2.com

Thanks!

EDIT: I'm using this

<script>
$('.demo select').change(function(){
 window.location.href="http://www.example.com";
 });
</script>
</head>

But still not working, and how to use to open different links:

Select option 1 = www.example1.com

Select option 2 = www.example2.com

itachi
  • 6,323
  • 3
  • 30
  • 40
Isleno Ituriel
  • 113
  • 1
  • 1
  • 6

4 Answers4

2

why not you try something like this

<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option selected>Selecione sua cidade</option>
    <option value='www.example1.com'>João Pessoa - PB</option>
    <option value='www.example2.com'>Outros</option>
</select>
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
  • 2
    You should use a function rather then dumping the entire code inline which will be tedious when OP wants to change across several pages – Mr. Alien Oct 27 '14 at 11:47
1

Have the urls as the values in the select, and call a function onchange:

<form class="demo">
<select onchange="go(this.value);">
    <option selected>Selecione sua cidade</option>
    <option value='http://someurl.com'>João Pessoa - PB</option>
    <option value='http://another.com'>Outros</option>
</select>
</form>

<script>
function go(url){
    window.location.href=url;
}
</script>
Steve
  • 20,703
  • 5
  • 41
  • 67
  • This is a decent Javascript-only answer. If you use jQuery however (as per the original), avoid using attribute-based events entirely as the separate the registration and handler code needlessly. – iCollect.it Ltd Oct 27 '14 at 11:51
1

You can use Jquery val() function to check which value was selected.

form:

<form class="demo">
<select>
   <option selected>Selecione sua cidade</option>
   <option value='http://example.a'>João Pessoa - PB</option>
   <option value='http://example.b'>Outros</option>
</select>
</form>

<script>
$(function (){
  $('.demo select').change(function(){
    window.location.href = $(this).val();
  }
});
</script>
Robert
  • 19,800
  • 5
  • 55
  • 85
0

Please try something like this:

<form class="demo">
<select id='drop'>
    <option selected>Selecione sua cidade</option>
    <option value='JP'>João Pessoa - PB</option>
    <option value='outros'>Outros</option>
</select>
</form>

<script>
 $("#drop").change(function () {
    var res = this.value;
    if(res=='JP')
    {
            document.location.href = "http://example.com";
    }
    else if(res == 'outros')
    {
      document.location.href = "http://example2.com";
    }
    });
</script>
AkshayP
  • 2,141
  • 2
  • 18
  • 27