JS action before the form is submitted

Source: Internet
Author: User
Tags button type

Recently, when writing a page, you need to manually write some validation before the form is submitted, just see 2 ways to block form submission, you can do some logical processing

Method One: Use return False

Native JS notation:<form id= "LoginForm" name= "LoginForm" action= "Login.aspx" method= "POST" > <button type= "Submit" value= "submit "Id=" >Submit</button></form><script> "Submit"varSUBMITBTN = document.getElementById ("Submit"); Submitbtn.onclick=function(Event) {alert ("Preventdefault!"); return false; };</script>jquery notation:<script>$("#loginForm"). Submit (function(){if(condition) {//Here's the logical processing here .}Else{    return false;}})</script>//one of the problems found here is that when you use the JQ notation, as long as you do not return FALSE, the logical processing will automatically submit the form

Method Two: Use Preventdefault ()

var submitbtn = document.getElementById ("Submit"function  (event) {  alert (" Preventdefault! " );   var event = Event | | window.event;   // compatible with standard browsers  false // compatible with ie6~8  }; </script>

JS action before the form is submitted

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.