2011-04-11 48 views
0

Ello堆棧,無法似乎弄清楚爲什麼沒有任何文本顯示在我的div驗證後的字段。這是我的代碼。我知道驗證正在運行,但是,我不確定爲什麼我的驗證消息沒有顯示在div中。 不知道爲什麼我的div沒有顯示驗證消息

<html> 
<head> 
    <script src="Scripts/jquery-1.5.2.js" type="text/javascript"></script> 
    <script src="http://localhost:62147/Scripts/jquery.validate.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     "use strict"; 
     $(document).ready(function() { 

      $("#testerbtn").bind("click", function() { 
       alert("onclick is fired"); 
       $("#myform").validate({ 
        errorLabelContainer: $("#Errors"), 
        rules: { 
         textToVal: { required: true } 
        }, 
        messages: { 
         textToVal: "the field is required! nub~!" 
        } 
       }); 
       alert("validation should have happend."); 

      }) 
     }); 

    </script> 
    <title>Test Page</title> 
</head> 
<body> 
    <form id = "myform" action="htmlPage1.htm"> 
     <div id="Errors"></div> 
     <div> 
      <input type="text" id="textToVal" value="" /> 
      <input type="button" id="testerbtn" value="Tester" /> 
     </div> 
    </form> 
</body> 
</html> 
+0

'.validate()'有回調嗎? – Blender 2011-04-11 16:45:10

+0

確定要驗證嗎?因爲如果這是複製粘貼的,你有''嚴格使用';'隨機放置在腳本的開頭。 – Khez 2011-04-11 16:47:09

+1

'「使用scrict」'不是一個隨機字符串。閱讀關於嚴格模式:https://developer.mozilla.org/en/JavaScript/Strict_mode – 2011-04-11 16:49:03

回答

7

有一對夫婦的事情,我看到這裏可能造成的問題:

  1. 確保您的input■找name屬性:

    <input type="text" id="textToVal" value="" name="textToVal" /> 
    
  2. validate()應該被實例化之外的點擊處理程序。此外,將您的button類型更改爲submit以自動觸發驗證碼。

總結:

的JavaScript:

$(document).ready(function() { 
    $("#myform").validate({ 
     errorLabelContainer: $("#Errors"), 
     rules: { 
      textToVal: { 
       required: true 
      } 
     }, 
     messages: { 
      textToVal: "the field is required! nub~!" 
     } 
    }); 
}); 

HTML:

<form id = "myform" action="htmlPage1.htm">   
    <div id="Errors"></div>   
    <div>    
     <input type="text" id="textToVal" value="" name="textToVal" />    
     <input type="submit" id="testerbtn" value="Tester" />   
    </div>  
</form> 

工作實施例:http://jsfiddle.net/fJ8XF/

+0

這是你的名單上的#2。你很多。 – Terrance 2011-04-11 17:20:30

0

HI特倫斯,

我相信errorLabelContainer意味着是一個選擇器(字符串),所以你應該使用 「#Errors」,而不是$('#Errors')

+0

試過這個'errorLabelContainer:#錯誤,'並且得到了一個無效的字符錯誤。 – Terrance 2011-04-11 17:20:02

相關問題