Forget about catching the enter key press, catch the form submit instead,
make it do the ajax call and prevent default response, and execute the same
ajax call on change :

var ajax_search = function() {
// whatever AJAX you want ...
};

$('#search').change(function(event) {
ajax_search();
})
.closest('form').submit(function(event) {
event.preventDefault();
ajax_search();
});

Hope it's what you need.

Michel Belleville


2009/10/26 cdukes77 <cduke...@bellsouth.net>

>
> I have the following syntax problem ...
>
>        $("#search").bind("keypress", function(e) {
>            if (e.keyCode == 13) return false;
>        });
>
>
>        $('#search').change(function() {
>            var vKeyword = document.getElementById('search').value
>            $.ajax({
>                type: "GET",
>                ...
>
> The first little bit of code traps for a user striking the enter key
> and cancels it (this prevents the page from submitting like a form).
>
> The second code snippet is the beginning of an AJAX script to get
> information based on a keyword value in a text field with an id of
> "search" which triggers when the change event fires.
>
> Both work perfectly ... but I would like to combine the two in such a
> way as to trigger the AJAX script on both/either the change event or
> the enter key - and still have the enter key NOT submit the page. I
> just can't figure out how to do it in a syntactically tidy way ...
> anybody have a suggestion?
>

Reply via email to