Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an extra parameter to Jquery Autocomplete field?

I'm using the JQuery Autocomplete in one of my forms.

The basic form selects products from my database. This works great, but I'd like to further develop so that only products shipped from a certain zipcode are returned. I've got the backend script figured out. I just need to work out the best way to pass the zipcode to this script.

This is how my form looks.

<form>  <select id="zipcode">  <option value="2000">2000</option> <option value="3000">3000</option> <option value="4000">4000</option>  </select>  <input type="text" id="product"/>  <input type="submit"/>  </form> 

And here is the JQuery code:

$("#product").autocomplete ({      source:"product_auto_complete.php?postcode=" + $('#zipcode').val() +"&",      minLength: 2,      select: function(event, ui){                               //action                                   } }); 

This code works to an extent. But only returns the first zipcode value regardless of which value is actually selected. I guess what's happening is that the source URL is primed on page load rather than when the select menu is changed. Is there a way around this? Or is there a better way overall to achieve the result I'm after?

like image 212
matt Avatar asked Sep 12 '10 02:09

matt


People also ask

How to pass extra parameter in jQuery Autocomplete?

In that case, you need to define a callback function to the source option of autoComplete call. Also, you can pass multiple parameters using this approach. Your jQuery UI Autocomplete function would be like the following. In the source file, you can get the term and additional parameters using the $_GET variable.

How does autocomplete work in jQuery?

The autocomplete (options) method declares that an HTML <input> element must be managed as an input field that will be displayed above a list of suggestions. The options parameter is an object that specifies the behavior of the list of suggestions when the user is typing in the input field.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })


1 Answers

You need to use a different approach for the source call, like this:

$("#product").autocomplete({   source: function(request, response) {     $.getJSON("product_auto_complete.php", { postcode: $('#zipcode').val() },                response);   },   minLength: 2,   select: function(event, ui){     //action   } }); 

This format lets you pass whatever the value is when it's run, as opposed to when it's bound.

like image 141
Nick Craver Avatar answered Sep 24 '22 12:09

Nick Craver