Home >Web Front-end >JS Tutorial >How to Prevent Web Form Submission on ENTER Keypress?
Preventing Web Form Submission on ENTER Keypress
When designing web-based applications, it's often desirable to prevent the ENTER key from submitting a form. This can be accomplished using JavaScript.
Solution:
Revision 2012 ( Inline Handler)
Define a function checkEnter to handle the ENTER keypress:
Assign this handler to the form:
This prevents submission on ENTER for all elements except textareas.
Revision 2024 ( Event Delegation)
Implement a more modern approach using event delegation:
Add data-enter-for-submit attribute to elements you want to submit on ENTER:
The above is the detailed content of How to Prevent Web Form Submission on ENTER Keypress?. For more information, please follow other related articles on the PHP Chinese website!