欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS兩種類(lèi)型的表單提交方法實(shí)例分析

 更新時(shí)間:2016年11月28日 11:35:51   作者:巴霍巴利  
這篇文章主要介紹了JS兩種類(lèi)型的表單提交方法,結(jié)合實(shí)例形式分析了2種常用的表單提交驗(yàn)證的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例分析了JS兩種類(lèi)型的表單提交方法。分享給大家供大家參考,具體如下:

1.原始的

<form method="post" action="/student/stureg/add" id="form1" onsubmit="return subForm();">
<button type="submit" class="button red" style="font-size:18px; font-family:'微軟雅黑';">提&nbsp;交</button>

這里的button提交之后,執(zhí)行subForm()方法,subForm可以對(duì)表單進(jìn)行驗(yàn)證,返回false,表單不提交。否則提交。

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("此處為必填項(xiàng),請(qǐng)您填寫(xiě)!");
    }
  });
 /*$(".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("請(qǐng)?zhí)顚?xiě)正確的身份證號(hào)碼!");
   }
  }
 });*/
 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("請(qǐng)?zhí)顚?xiě)正確的聯(lián)系電話!");
   }
  }
 });
 $(".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("此身份證已報(bào)名!");
   }
  }
 });
  return flag;
}

各種驗(yàn)證!

2.js設(shè)置的提交

<form method="post" action="/student/stureg/reglogin" id="form_login">
<a id="submit"><img src="/images/login/login_bt.png" style="cursor:pointer"/></a>

這里并不是提交按鈕,而是一個(gè)模擬提交的按鈕。

$("#submit").click(function(){
   if(loginForm())
   {
     $("#form_login").submit();
   }
});

觸發(fā)提交事件,這里用

onsubmit="return loginForm();"就沒(méi)效果了,不管是否返回false都會(huì)提交。所以要在真正提交之前,做一下驗(yàn)證。

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;
}

返回false,就不調(diào)用submit方法。

這就是兩種處理表單提交前奏的方式。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論