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
Release: 2024-11-17 21:02:02
Original
386 people have browsed it

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template