WonderBugger
WonderBugger

Reputation: 275

jquery - "click" on keypress?

I have an ajax search form that works fine if you click "submit" in that you enter a zipcode, click the "Enter" button and up pops results instantly. I'm trying to make it so that if the user presses "enter" on the keyboard, it has the same effect. It looks like it's submitting, but no results come up...

I tried:

  if($('#search').length) {
        $('#zipcode').keyup(function(e) {
          if(e.keyCode == 13) {
        e.preventDefault();
            $('#search').submit();
          }
        });
  }

Upvotes: 0

Views: 1794

Answers (2)

David Tang
David Tang

Reputation: 93684

You shouldnt have to "fake" a click. Make sure your "enter" button is either:

<input type="submit">

Or

<button type="submit">

And it should submit the form on pressing enter.

The other potential problem is that the Ajax code is bound to the button's click event, rather than the form's submit event.

$('button').click(...); // is wrong
$('form').submit(...); // is right 

Upvotes: 2

sethvargo
sethvargo

Reputation: 26997

Use .click() with no parameters

Upvotes: 1

Related Questions