<!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>Validation插件success和submitHandler两个方法区别-Mzwu.COM</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$("#form1").validate({
errorClass: "error",
errorElement: "div",
errorPlacement: function(error, element) {
element.after(error);
},
rules: {
username: { required: true, minlength: 6},
password: { required: true, minlength: 6}
},
messages: {
username: { required: "必填", minlength: $.validator.format("不得少于{0}字符.")},
password: { required: "必填", minlength: $.validator.format("不得少于{0}字符.")}
},
success: function(label) {
alert("success");
},
submitHandler: function(form){
alert("submit");
form.submit(); //没有这一句表单不会提交
}
});
});
</script>
<form id="form1" name="form1" method="post" action="">
username:<input type="text" name="username" id="username" /><br />
password:<input type="password" name="password" id="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validation插件success和submitHandler两个方法区别-Mzwu.COM</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$("#form1").validate({
errorClass: "error",
errorElement: "div",
errorPlacement: function(error, element) {
element.after(error);
},
rules: {
username: { required: true, minlength: 6},
password: { required: true, minlength: 6}
},
messages: {
username: { required: "必填", minlength: $.validator.format("不得少于{0}字符.")},
password: { required: "必填", minlength: $.validator.format("不得少于{0}字符.")}
},
success: function(label) {
alert("success");
},
submitHandler: function(form){
alert("submit");
form.submit(); //没有这一句表单不会提交
}
});
});
</script>
<form id="form1" name="form1" method="post" action="">
username:<input type="text" name="username" id="username" /><br />
password:<input type="password" name="password" id="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
Validation插件success和submitHandler两个方法区别
success:当表单元素验证成功时执行,对于同一个表单元素,由于onkeyup、onfocusout、onsubmit等事件都会触发验证,所以本方法可能会多次执行;
submitHandler:当表单验证成功并提交时执行,存在此方法时表单只能在此方法内部执行form.submit()才能提交,可理解成它替代了表单的onsubmit方法;