I have an ajax search form that wor开发者_Go百科ks 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();
}
});
}
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
Use .click() with no parameters
精彩评论