P粉4574458582023-08-21 12:43:06
使用prevent default
Dojo Toolkit
dojo.connect(form, "onsubmit", function(evt) { evt.preventDefault(); window.history.back(); });
jQuery
$('#form').submit(function (evt) { evt.preventDefault(); window.history.back(); });
Vanilla JavaScript
if (element.addEventListener) { element.addEventListener("submit", function(evt) { evt.preventDefault(); window.history.back(); }, true); } else { element.attachEvent('onsubmit', function(evt){ evt.preventDefault(); window.history.back(); }); }
P粉7948519752023-08-21 10:22:41
You can use the return value of the function to prevent the form from being submitted
<form name="myForm" onsubmit="return validateMyForm();">
And the function is as follows:
<script type="text/javascript"> function validateMyForm() { if(check if your conditions are not satisfying) { alert("validation failed false"); returnToPreviousPage(); return false; } alert("validations passed"); return true; } </script>
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:
<form onsubmit="event.preventDefault(); validateMyForm();">
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