为什么我跟着老师写的网页,只是把js代码分开一个文件放之后username获取的时候报错? $("#username").rules() jquery.validate-1.13.1.js:961 Uncaught TypeError: Cannot read property 'getAttribute' of undefined(…)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src="vendor/jquery-1.10.0.js"></script> <script src="vendor/jquery.validate-1.13.1.js"></script> <script type="text/javascript" src="script.js"></script> </head> <body> <form id="demoForm"> <fieldset> <legend>用户登陆</legend> <p id="info"></p> <p id="info2" style="display:none">输入错误</p> <p> <label for="username">用户名</label> <input type="text" id="usename" name="username" /> </p> <p> <label for="password">密码</label> <input type="password" id="password" name="password" /> </p> <p> <label for="confirm-password">确认密码</label> <input type="password" id="confirm-password" name="confirm-password"/> </p> <p> <input type="submit" value="登录" /> </p> </fieldset> </form> </body> </html>
var validator1; $(function(){ validator1 = $("#demoForm").validate({ debug: true, rules: { username: { required: true, minlength:2, maxlength:10, }, password:{ required: true, minlength:2, maxlength:16, }, "confirm-password": { equalTo:"#password", } }, messages:{ username:{ required:'请输入用户名', minlength:'用户名能小于2个字符', maxlength:'用户名不能大于10个字符', }, password:{ required:'请输入密码', minlength:'密码不能小于2个字符', maxlength:'密码不能超过16个字符', }, "confirm-password":{ equalTo:'两次输入密码不一致' } }, }) })
id写错了
我知道了,id写错了