Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery to navigate to page in select option

Tags:

jquery

I am trying to write a little snippet that allows a user to go to i.e. navigate to) a new url, based on a user selection in an option select control.

<script type="text/javascript">
/* <[CDATA[ */
jQuery.noConflict();
jQuery(document).ready(function(){
    var id = jQuery($this).val();
    var url = some_Lookup_func(id);
    jQuery("#the_id").change(function () { /* how do I navigate the browser to 'url' ?*/ );
});
/* ]]> */
</script>

Note: This is not AJAX behaviour I want, I want the browser to behave as though you had clicked on a hyperlink. I have done this before, but I have forgotten how to do it. I had a look at the jQuery docs, and load() does not seem to do it - because I do not want to place the contents in the current page - I want to:

  1. go to an entirely new page
  2. pass parameters to the url that I am navigating to (e.g. the id of the selected item
like image 658
morpheous Avatar asked Jun 19 '10 14:06

morpheous


2 Answers

jQuery

var YourParam="sunshine";

$(document).ready(function() {
  $("#goto").change(function(){
    if ($(this).val()!='') {
      window.location.href=$(this).val()+"?param="+YourParam;
    }
  });
});

HTML

<form action="whatever.shtml" method="post" enctype="multipart/form-data">
  <select id="goto">
    <option value="">Go somewhere...</option>
    <option value="http://cnn.com/">CNN</option>
    <option value="http://disney.com/">Disney</option>
    <option value="http://stackoverflow.com/">stackoverflow</option>
    <option value="http://ironmaiden.com/">Iron Maiden</option>
  </select>
</form>
like image 110
Gert Grenander Avatar answered Oct 16 '22 08:10

Gert Grenander


window.location.href = 'http://example.com/newlocation?param1=value1';

This also works with relative url:

window.location.href = '/newlocation?param1=value1';
like image 38
Darin Dimitrov Avatar answered Oct 16 '22 08:10

Darin Dimitrov