JavaScript code that prevents form submission
P粉163465905
2023-08-20 13:58:50
<p>One way to stop form submission is to return false from your JavaScript function. </p>
<p>When the submit button is clicked, a validation function is called. I have a condition in form validation. If that condition is met, I call a function called <strong>returnToPreviousPage();</strong>. </p>
<pre class="brush:php;toolbar:false;">function returnToPreviousPage() {
window.history.back();
}</pre>
<p>I'm using JavaScript and the Dojo Toolkit. </p>
<p>Instead of returning to the previous page, it submits the form. How do I abort this submission and return to the previous page? </p>
使用prevent default
Dojo Toolkit
jQuery
Vanilla JavaScript
You can use the return value of the function to prevent the form from being submitted
And the function is as follows:
If the above code does not work in Chrome 27.0.1453.116 m, please set the event handler's parameter returnValue field to false .
Thanks Sam for sharing the information.
edit:
Thanks to Vikram for the solution, if validateMyForm() returns false:
Where validateMyForm() is a function that returns false when validation fails. The key point is to use the name event. We cannot use e.preventDefault()
for example