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

1

22

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?

Dulciana answered 9/5, 2011 at 16:32 Comment(1)
I wanted to do this in rails 3.1 finally figured it out and put it in a Gist on githubSpinal
G
9

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

Gross answered 9/5, 2011 at 16:46 Comment(2)
Wow, I actually was missing it. Haha, hours of trying to figure this out and that was the error. Thanks!Dulciana
I think every programmer has been there some time. I have personally spent days in the past debugging just to find errors like this.Gross

© 2022 - 2024 — McMap. All rights reserved.