I have a button (<input type="submit">
). When it is clicked the page reloads. Since I have some jQuery hide()
functions that are called on page load, this causes these elements to be hidden again. How do I make the button do nothing, so I can still add some action that occurs when the button is clicked but not reload the page.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
You can use a form that includes a submit button. Then use jQuery to prevent the default behavior of a form:
In HTML:
in order to avoid refresh at all "buttons", even with onclick assigned.
Use either the
<button>
element or use an<input type="button"/>
.As stated in one of the comments (burried) above, this can be fixed by not placing the button tag inside the form tag. When the button is outside the form, the page does not refresh itself.
I can't comment yet, so I'm posting this as an answer. Best way to avoid reload is how @user2868288 said: using the
onsubmit
on theform
tag.From all the other possibilities mentioned here, it's the only way which allows the new HTML5 browser data input validation to be triggered (
<button>
won't do it nor the jQuery/JS handlers) and allows your jQuery/AJAX dynamic info to be appended on the page. For example:In HTML:
With jQuery, some similar variant, already mentioned.