Home  >  Article  >  Web Front-end  >  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
Mary-Kate OlsenOriginal
2024-10-26 13:12:29843browse

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!

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