Submitting a Form on 'Enter' With Jquery

Submitting a form on 'Enter' with jQuery?

$('.input').keypress(function (e) {
if (e.which == 13) {
$('form#login').submit();
return false; //<---- Add this line
}
});

Check out this stackoverflow answer:
event.preventDefault() vs. return false

Essentially, "return false" is the same as calling e.preventDefault and e.stopPropagation().

jquery submit using button or enter?

this works with button and pressing "enter"

<form id="myForm">
<input id='search' name='search' />
<input type='submit' value='Find'>
</form>

$("#myForm").submit(function( event ) {
event.preventDefault();
// your code here
});

Submit a JQuery form with Enter key press

I'll give it a go, a more complete solution for the whole 'Listen to Keypress Enter to submit' which I faced some problems while making it work cross-browser, perhaps this will do it for you.

According to your code, this should work just fine.

  $('#inputbox').live("keypress", function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
e.preventDefault();
e.stopPropagation();
$(this).closest('form').submit();
}
});

Prevent users from submitting a form by hitting Enter

You can use a method such as

$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});

In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:

function validationFunction() {
$('input').each(function() {
...

}
if(good) {
return true;
}
return false;
}

$(document).ready(function() {
$(window).keydown(function(event){
if( (event.keyCode == 13) && (validationFunction() == false) ) {
event.preventDefault();
return false;
}
});
});

jquery disable form submit on enter

If keyCode is not caught, catch which:

$('#formid').on('keyup keypress', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
e.preventDefault();
return false;
}
});

EDIT: missed it, it's better to use keyup instead of keypress

EDIT 2: As in some newer versions of Firefox the form submission is not prevented, it's safer to add the keypress event to the form as well. Also it doesn't work (anymore?) by just binding the event to the form "name" but only to the form id. Therefore I made this more obvious by changing the code example appropriately.

EDIT 3: Changed bind() to on()

How do I submit a form on keypress 'enter' with jQuery?

If you want submit your form using the Enter key, use something like :

$(document).ready(function() {
$('#inputbox').keypress(function(e) {
e.preventDefault();
if (e.keyCode == 13) {
$('#testt').submit();
}
});
});

To listen on the keypress event, out of any function.
If you are focus on the #inputbox input, the logic inside the condition will be called.

EDIT

It's just perfectly working.

I have made a fiddle for you, that call the on-submit event of your form on hit enter in your #inputbox :

Fiddle

jQuery button submit with enter key function

Check this demo http://jsfiddle.net/yeyene/hd7zqafg/

First click on the result frame (because there are another frames), and press enter, the submit button click event will fire on Enter key press & on button click.

$(document).ready(function()               
{
// enter keyd
$(document).bind('keypress', function(e) {
if(e.keyCode==13){
$('#button_sign_in').trigger('click');
}
});

$('#button_sign_in').click(function(){
alert('You click submit!');
console.log('login');
$.ajax
({
url: "login",
type: "post",
data: $('#login_form').serialize(),
dataType:'json',
success: function (data)
{
if (data.status=='SUCCESS')
{
window.location='home.php';
}
else
{
$('#button_sign_in').shake(4,6,700,'#CC2222');
$('#username').focus();
}
},
error: function (e) {
console.log('error:'+e);
}
});
});
});


Related Topics



Leave a reply



Submit