0

Hi have been looking around how to do something like this with jquery or JavaScript but can't seem to find it.

So let's say I have a form with a select field in it.

Ex:

<select id="Country">
    <option value="USA">USA</option>
    <option value="Canada">Canada</option>
    Etc.....
</select>

All I want is when user selects the option with value="Canada" To have them redirected to a different URL. Note: I can't modify the I need to be able to have jquery/javascript see value Canada was selected and send them to a different URL as soon as they make that selection.

  • 2
    What have you tried so far... I am sure there are many solutions already for such problem.. – Rajshekar Reddy Feb 28 '17 at 15:05
  • @Kudzai you are wrong.. – Rajshekar Reddy Feb 28 '17 at 15:07
  • http://stackoverflow.com/questions/5150363/onchange-open-url-via-select-jquery and http://stackoverflow.com/questions/12388954/redirect-form-to-different-url-based-on-select-option-element might help.. there is some small changes you need to add into the code ... take that as a homework. – Rajshekar Reddy Feb 28 '17 at 15:08
  • Do you have to generate the URL as well? I noticed that you can't change the HTML, and that there is no URL stored with each option, so if I select Canada do you have to generate a URL such as /countries/Canada ? – 01010011 01000010 Feb 28 '17 at 15:10
  • No all I need is to send them to a url that can be specified in the javascript only if they select Canada. – user7635584 Feb 28 '17 at 19:05

2 Answers2

1

You can trigger event on Change like this:

 $(document).ready(function() { 
    $('#Country').on('change', function(){
        window.location.href = this.value; //USA, Canada
    }); 
 });
Roy Bogado
  • 4,299
  • 1
  • 15
  • 31
0

Try something like this:

$(document).ready(function(){
$("#Country").change(function(){
window.location.href=$('option :selected').value;
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Country">
    <option value="USA">USA</option>
    <option value="Canada">Canada</option>
    Etc.....
</select>
Hemant
  • 1,961
  • 2
  • 17
  • 27
  • This worked: – user7635584 Feb 28 '17 at 22:21