[英]Jquery validation is not working using jquery validation plugin
我嘗試使用jquery驗證插件進行簡單驗證,但無法實現。單擊“提交”按鈕沒有任何改變,請幫助我
我已經提到了以下代碼,
提前致謝。
<!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>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js" />
<script type="text/javascript">
$(document).ready(function () {
$('#btnSubmit').click(function () {
$('#logform').validate({
rules: {
txtUsr: {
required: true
},
txtPass: {
required: true
}
},
messages: {
txtUsr: {
required: 'Please enter username'
},
txtPass: {
required: 'Please enter password'
}
}
});
});
});
</script>
</head>
<body>
<div id='LoginDiv' style='width:400px;height:300px;background-color:green;'>
<form method="post" action="" id='logform'>
User Name : <input type="text" id='txtUsr' name='txtUsr' style='margin-top:12px'/>
<br/>
<br/>
Password : <input type="text" id='txtPass' name='txtPass' />
<br/>
<br/>
<input type="submit" id='btnSubmit' value='Submit' />
</form>
</div>
</body>
</html>
您不應在click事件處理程序中調用validate,而應作為初始化步驟:
$(document).ready(function () {
$('#logform').validate({
rules: {
txtUsr: {
required: true
},
txtPass: {
required: true
}
},
messages: {
txtUsr: {
required: 'Please enter username'
},
txtPass: {
required: 'Please enter password'
}
}
});
});
它將自己連接到keyup並提交事件。
http://jsfiddle.net/adiioo7/FFTkJ/
采用
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js"></script>
代替
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js" />
還要檢查一下為什么自關閉腳本標簽不起作用?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.