In JavaScript, there are two ways to block the default submission behavior of a 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) using Preventdefault ()
In standard browsers, the default behavior of the browser is blocked, event.preventDefault()
and in ie6~8, returnValue
attributes are used to implement it.
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 standard browser
window.event.returnValue = false;//compatible ie6~8
};
</script>
These are the full contents of the two methods that use JavaScript to block form submissions, and hopefully the content of this article will help you to use JavaScript.