In JavaScript, there are two ways to prevent the default submission behavior of form, namely:
(1) return false
Sample code
<form name="loginForm" action="login.aspx" method="post"> <button type="submit" value="Submit" id="submit">Submit</button></form><script> var submitBtn = document.getElementById("submit"); submitBtn.onclick = function (event) { alert("preventDefault!"); return false; };</script>(2) Use preventDefault()
In standard browsers, the default behavior of the browser is blocked using event.preventDefault() , while in IE6~8, returnValue property is implemented.
Sample code
<form name="loginForm" action="login.aspx" method="post"> <button type="submit" value="Submit" id="submit">Submit</button></form><script> var submitBtn = document.getElementById("submit"); submitBtn.onclick = function (event) { alert("preventDefault!"); var event = event || window.event; event.preventDefault(); // Compatible with standard browsers window.event.returnValue = false; // Compatible with IE6~8 };</script>The above is all about the two methods of using JavaScript to prevent form submission. I hope the content of this article will be helpful to everyone using JavaScript.