Two ways to prevent form submission using JavaScript _javascript tips

Source: Internet
Author: User
Tags button type

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.