How to disable the "submit" behavior of a button label inside a form label?
P粉461599845
2023-08-24 15:00:36
<p><pre class="brush:php;toolbar:false;"><form action="">
<button>click</button>
</form></pre>
The default behavior of the <p> button within a form is to submit the form. </p>
<p>How do I disable the submit behavior of this button? </p>
Sometimes you want to preserve the details of a
submit
button (for example, on mobile devices, the button can only be accessed via the keyboard if it is part of a form and of typesubmit
Triggersubmit
button). In this case, you need to prevent the button's default behavior.Using jQuery 1.7 (using
.on
- I highly recommend reading the documentation - there are many nuances to using.on
that can greatly impact performance)Using an older version of jQuery...
Please note that you can use any selector (select by class, element type, etc.) or method of binding an event (instead of using
.bind
, use whatever works for you jQuery versions of.live
,.delegate
, etc.).Add
type="button"
above it:Available types are:
A good reference: https://developer.mozilla.org/en/DOM/HTMLButtonElement