Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing extra parameters to source using Jquery UI autocomplete [closed]

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?

like image 396
Brian Avatar asked May 09 '11 16:05

Brian


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.

What is the default value of Append to option of autocomplete () method?

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.

How does jQuery autocomplete work?

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.

What is UI autocomplete?

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.


1 Answers

Are you missing a # in your selector $("#id_city").val()?

like image 179
Robert Beuligmann Avatar answered Oct 16 '22 15:10

Robert Beuligmann