Submit form with enter

In any case, I can send the form without the submit button, but only when the user presses the "Enter" button? For example, when you comment on Facebook, you have TEXTAREA but no submit button. As soon as you click, enter the form submitted using Ajax.

Thanks,

Joel

+4
source share
2 answers

Set an onkeyup or onkeydown on your text field, the latter is better because it fires before a new line is added to the text field

 var $form = $( '#yourForm' ); $( '#yourTextArea' ).keydown(function( e ){ if( e.keyCode == 13 ){ $form.submit(); } }); $form.submit(function(){ alert( 'submitted' ); return false; }); 
+4
source

The following function checks if the key code is 13 or the Enter key is equal. If so, the function calls the submitformnow function.

 <script type="text/javascript"> function submitform(myfield, e) { var keycode; if (window.event) keycode = window.event.keyCode; else if (e) keycode = e.which; else return true; if (keycode == 13) { submitformnow(); return false; } else return true; } function submitformnow(){document.myform.submit();} </script> 

Then in your text box put onkeypress="return submitform(this, event)" .

0
source

All Articles