在浏览器中运行时,为什么我的jQuery验证代码无法运行?

时间:2015-05-23 11:25:43

标签: javascript jquery validation

它适用于jsfiddle。我不明白为什么当我在浏览器中运行它时它不会起作用。我唯一能想到的是我可能错误地链接了jquery文件/链接错误的文件。我尝过很多次了。基本上我去了jquery站点,下载了jquery,将它链接起来(它在同一个文件夹中)。我也尝试了具体的验证,没有用。现在我使用jsfiddle中的链接。请帮忙。 :(

<!DOCTYPE html>

   <html>
      <head>
         <title>JQ</title>

             <script src="validate.js"></script>
             <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>

     </head>
     <body>
             <form id="signupform">Username
             <input id="user" name="user" type="text" />
        <br/>
             <button type="submit">Submit</button>
             </form>

    </body>
 </html>

这是我的JS代码

$(document).ready(function () {
$("#signupform").validate({
    rules: {
        user: {
            required: true,
            minlength: 4
        }
    },
    messages: {
        user: {
            required: "please enter a username",
            minlength: "Your username must be at least 4 characters long"
        }
    }
});
});

1 个答案:

答案 0 :(得分:1)

您需要包含jQuery库

<!DOCTYPE html>

   <html>
      <head>
         <title>JQ</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script><!--Include jQuery before includign the plugin-->
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
        <script src="validate.js"></script>
     </head>
     <body>
             <form id="signupform">Username
             <input id="user" name="user" type="text" />
        <br/>
             <button type="submit">Submit</button>
             </form>

    </body>
 </html>