Home >Web Front-end >JS Tutorial >How to Prevent Web Form Submission on ENTER Keypress?

How to Prevent Web Form Submission on ENTER Keypress?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-17 21:02:02401browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn