Home > Web Front-end > JS Tutorial > How to Prevent a Button Outside a Form from Submitting It?

How to Prevent a Button Outside a Form from Submitting It?

Mary-Kate Olsen
Release: 2024-10-26 13:12:29
Original
908 people have browsed it

How to Prevent a Button Outside a Form from Submitting It?

Preventing Form Submission from a Button Outside the Form

Outside a form, it's possible to create a button with standard HTML, like this:

<button>My Button</button>

However, when clicked, this button often submits the nearest form, even though it's not enclosed within that form.

Solution

To prevent the button from submitting the form, set its type attribute to "button":

<button type="button">My Button</button>

This solution is based on the HTML specifications, which state that for a button without a specified type attribute, the default behavior is to submit the form. By explicitly setting it to "button," you can override this default behavior.

The above is the detailed content of How to Prevent a Button Outside a Form from Submitting It?. 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