Home > Article > Web Front-end > Simple implementation of JavaScript image switching effect
This article analyzes two types of form submission methods in JS through examples. Share it with everyone for your reference, the details are as follows:
1. After the original
<form method="post" action="/student/stureg/add" id="form1" onsubmit="return subForm();"> <button type="submit" class="button red" style="font-size:18px; font-family:'微软雅黑';">提 交</button>
button here is submitted, execute the subForm() method, subForm can verify the form, return false, and the form is not submitted. Otherwise submit.
function subForm() { var flag = true; $(".required").each(function(){ if(!$(this).val()) { flag = false; $(this).css({ border: "1px solid #F56939",borderRadius:"5px",color:"#F56939",height:"26px"}); $(this).attr("status","1").val("此处为必填项,请您填写!"); } }); /*$(".idCardNo").each(function(){ if(!idCardNo($(this).val())) { flag = false; $(this).css({ border: "1px solid #F56939",borderRadius:"5px",color:"#F56939",height:"26px"}); if($(this).attr("status")!=1){ $(this).attr("status","1").val("请填写正确的身份证号码!"); } } });*/ var reg = new RegExp("^[0-9]*$"); $(".number").each(function(){ if(!reg.test($(this).val())) { flag = false; $(this).css({ border: "1px solid #F56939",borderRadius:"5px",color:"#F56939",height:"26px"}); if($(this).attr("status")!=1){ $(this).attr("status","1").val("请填写正确的联系电话!"); } } }); $(".exCardNo").each(function(){ if(exCardNo($(this).val())==1) { flag = false; $(this).css({ border: "1px solid #F56939",borderRadius:"5px",color:"#F56939",height:"26px"}); if($(this).attr("status")!=1){ $(this).attr("status","1").val("此身份证已报名!"); } } }); return flag; }
Various verifications!
Submit set by 2.js
<form method="post" action="/student/stureg/reglogin" id="form_login"> <a id="submit"><img src="/images/login/login_bt.png" style="cursor:pointer"/></a>
This is not a submit button, but a simulated submit button.
$("#submit").click(function(){ if(loginForm()) { $("#form_login").submit(); } });
triggers the submission event. Using
onsubmit="return loginForm();" here has no effect. It will be submitted regardless of whether false is returned. So you need to do some verification before actually submitting it.
function loginForm(){ var flag = true; $(".notnull").each(function(){ if(!$(this).val()) { flag = false; $(this).css({ border: "1px solid #F56939",borderRadius:"5px",color:"#F56939",height:"26px"}); $(this).attr("status","1").val("不能为空"); } }); return flag; }
Return false and the submit method will not be called.
These are two ways to handle the prelude to form submission.