amanda23
amanda23

Reputation: 29

jQuery Dynamic form submission and Ajax call

everything works, I get my json array returned in an alert, I just need to change the onSubmit event handler $('#city').submit(function() to something more dynamic that grabs the user input and runs the ajax call as soon as the user types the letters.

Upvotes: 0

Views: 100

Answers (2)

Naryl
Naryl

Reputation: 1888

I'd recommend the keyup() event:

$("#term").keyup(function(e){

});

But you can also use the autocomplete function from JQuery-UI: autocomplete

Using autocomplete this would be:

$("#term").autocomplete({source: "/suggestjson", minLength: 2, select: function (event, ui) {
    //do something when the user selects, by the way the value 
    //selected by the user is in: 'ui.item.value'
}});

Upvotes: 1

Kiren S
Kiren S

Reputation: 3097

Use

 $('#city').change(function() {

        var formdata = $('#term').val() 

        $.ajax({
            url: "/suggestjson",
            type: "GET",
            dataType: "json",
            data: {'term': formdata},
            success: function (data) {
                alert(data);
            }   
        }); 

        return false;
    });

Or

 $('#city').keyup(function() { 
     ........
     .......
    });

Upvotes: 0

Related Questions