I'm trying to pass extra parameters for city and state using the jQuery UI autocomplete function. I've been trying to find an answer to this for a while but can't seem to find something that works for me.
My current code is:
$(document).ready(function () {
$("#id_place").autocomplete({
source: function(request, response) {
$.ajax({
url: "/autocomplete_place",
dataType: "json",
data: {
term: request.term,
city: $("id_city").val(),
state: $("id_state").val(),
test: 4
},
success: function(data) {
response(data);
}
});
},
});
});
The autocomplete works, but its not passing my city and state parameters to the function. If I type v
it requests the URL: /autocomplete_place?term=v&test=4
I'm guessing its evaluating the val()
of city and state upon (document).ready()
and getting blank values for these form fields? I thought making source into an ajax function would solve that, but perhaps not.
Any ideas?
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.
By default its value is null. This option is used append an element to the menu. By default its value is null. When the value is null, the parents of the input field will be checked for a class of ui-front.
In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.
Autocomplete mechanism is frequently used in modern websites to provide the users a list of suggestion while typing the beginning word in the text box. It facilitates the user to select an item from the list, which will be displayed in the input field.
Are you missing a # in your selector $("#id_city").val()?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With