亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jquery.validate使用時(shí)遇到的問題

 更新時(shí)間:2015年05月25日 09:25:41   投稿:hebedich  
這篇文章主要介紹了jquery.validate使用時(shí)遇到的問題的解決方法的匯總,真是應(yīng)了那句老話,會(huì)者不難難者不會(huì),這里記錄一下,有需要的小伙伴可以參考下。

問題一:

<script src="../js/jquery.js"></script>
<script src="../js/jquery.validate.js"></script>
<script>
 $().ready(function() {
  $("#registerForm").validate();
 });
</script>
 
<form id="registerForm" method="get" action="">
 <fieldset>
  <p>
   <label for="cusername">用戶名</label>
   <input id="cusername" name="username" type="text" data-rule-required="true" data-rule-rangelength="[2,10]" data-msg-required="用戶名不能為空" data-msg-rangelength="用戶名長度必須是2到10個(gè)字符">
  </p>
  <p>
   <label for="cpassword">密碼</label>
   <input id="cpassword" name="password" type="password" data-rule-required="true" data-rule-minlength="6" data-msg-required="密碼不能為空" data-msg-minlength="至少設(shè)置6位密碼">
  </p>
  <p>
   <label for="cconfirmpassword">確認(rèn)密碼</label>
   <input id="cconfirmpassword" name="confirmpassword" type="password" data-rule-equalTo="#cpassword" data-msg-equalTo="兩次密碼不一致">
  </p>
  <p>
   <label for="cemail">郵箱</label>
   <input id="cemail" name="email" data-rule-required="true" data-rule-email="true" data-msg-required="郵箱不能為空" data-msg-email="郵箱的格式不正確">
   </input>
  </p>
  <p>
   <label for="chasreferee">有推薦人請(qǐng)勾選</label>
   <input type="checkbox" id="chasreferee" name="hasreferee">
  </p>
  <p>
   <label for="creferee">推薦人</label>
   <input id="creferee" name="referee" data-rule-required="#chasreferee:checked" data-msg-required="推薦人不能為空">
   </input>
  </p>
  <p>
   <input type="submit" value="提交">
  </p>
 </fieldset>
</form>

看了之前的別人寫的文章,貌似是依賴jquery.metadata.js這個(gè)庫,然后寫的時(shí)候以 class=”required email” 這樣的形式來寫,這樣寫起來好像有些亂,class本身是呈現(xiàn)樣式的,現(xiàn)在被附上各種校驗(yàn)的規(guī)則,看上去似乎有些亂,不過好在新版本中,又有了新的寫法,不依賴上面的js庫,以 data-rule-驗(yàn)證規(guī)則、data-msg-提示信息 這樣的格式來重新定義,更簡(jiǎn)單,更直觀,更強(qiáng)大了。上面的測(cè)試通過

我的版本的jquery.validate1.13.js

然后這樣的寫法,控件中的messages不會(huì)生效,還會(huì)報(bào)錯(cuò):Cannot read property 'call' of undefined 園子里面很多jquery.validate文章提到可以使用,我看是版本過時(shí)了,反正我沒有試驗(yàn)出來。還有就是將驗(yàn)證卸載class里面我也是醉了。下面的測(cè)試錯(cuò)誤!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript" src="jquery.min.js"></script>
 
<!--<script type="text/javascript" src="jquery.validate.js"></script>-->
<script type="text/javascript" src="jquery.validate1.13.js"></script>
<script type="text/javascript" src="jquery.validate.message_cn.js"></script>
<script type="text/javascript" src="jquery.metadata.js"></script>
<script type="text/javascript">
$(function(){
  $.metadata.setType("attr", "validate");
  $("#signupForm").validate();
  //$("#signupForm").validate({ meta: "validate" });
  //$("#commentForm").validate();
})
 
</script>
</head>
 
<body>
<form id="signupForm" method="get" action="">
  <p>
 
 
<input id="email" name="email" validate="{required:true, email:true, messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}" />
  </p>
 
  <p>
    <input class="submit" type="submit" value="Submit"/>
  </p>
</form>
 
</body>
</html>

問題二:jQuery_validate配置后無論怎樣都看不到提示信息。

原因:submit()了二次。

例子:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
<title>jquery test</title> 
<script src="js/jquery.js"></script> 
<script src="js/jquery.validate.js"></script> 
<script src="js/jquery.metadata.js"></script> 
<script src="js/messages_zh.js"></script> 
 
<script> 
$(document).ready(function() { 
     
$("#commentForm").validate({ 
             //debug:true 
             }); 
}); 
</script> 
<script type="text/javascript"> 
  function register(){ 
    document.forms[0].action = 'register/addUser.action'; 
    //document.forms[0].submit(); 
  } 
</script> 
</head> 
<body > 
  <form id="commentForm" method="post"> 
    <table style ="width:100%"> 
      <tr> 
        <td>user name:</td> 
        <td><input type="text" name="username" id="username" maxlength="10" 
          class="{required:true,minlength:6,maxlength:12}" /></td> 
      </tr> 
      <tr> 
        <td>password:</td> 
        <td><input type="password" name="password" id="password" maxlength="15" 
          class="required"/></td> 
      </tr> 
      <tr> 
        <td></td> 
        <td><input type="submit" value="Register" onclick="register();"></td> 
      </tr> 
    </table> 
  </form> 
</body> 
</html> 

jQuery驗(yàn)證后有個(gè)提交,自己在register()中又提交了一次,將【document.forms[0].submit();】注釋掉后問題解決。

相關(guān)文章

最新評(píng)論