jQuery.validate errorPlacement和success共用有问题
使用了success后呢 不论验证通过还是没有通过 errorPlacement都会被触发...
而且是先触发errorPlacement函数 再触发success函数
去掉success就很正常 只有出错时才会触发errorPlacement
这里是官方文档 http://jqueryvalidation.org/validate#errorPlacment
没看到这里说他们有冲突啊
另外我还参考了http://blog.51yip.com/jsjquery/1206.html
这是我的代码 不知道哪里有问题呢
<div id="em"></div>
<form id="signup-form" action="/account/signup" method="post" class="common-form">
<div id="yui_3_12_0_1_1389079738187_71"
class="field-group field-group--password">
<label for="signup-password">创建密码</label> <input name="password"
id="password" class="f-text" autocomplete="off" type="password">
</div>
<div id="yui_3_12_0_1_1389079738187_73" class="field-group">
<label for="password2">确认密码</label> <input name="password2"
id="password2" class="f-text" autocomplete="off" type="password">
</div>
<div class="field-group operate">
<input class="btn" name="commit" value="同意以下协议并注册" type="submit">
</div>
</form>
$(function() {
$("#signup-form").validate({
errorElement : "span",
//失去焦点时验证
onfocusout : function(element) {
$(element).valid();
},
onkeyup : false,
focusInvalid : false,
rules : {
password : {
required : true,
minlength : 5
},
password2 : {
required : true,
minlength : 5,
equalTo : "#password"
}
},
messages : {
password : {
required : "请输入密码",
minlength : jQuery.format("密码不能小于{0}个字 符")
},
password2 : {
required : "请输入确认密码",
minlength : "确认密码不能小于5个字符",
equalTo : "两次输入密码不一致不一致"
}
},
success:function(element){
$("#em").text($("#em").text()+'success');
},
errorPlacement : function(error, element) {
//error是错误提示元素span对象 element是触发错误的input对象
//发现即使通过验证 本方法仍被触发
//当通过验证时 error是一个内容为空的span元素
$("#em").text($("#em").text() + "error value is " + error.text());
element.nextAll('span').remove();
error.insertAfter(element);
}
});});
我也遇到过 可能是语句有错误 你再好好检查下 ,编译通过不代表没错
我觉得和success的默认值有关。
success默认是一个字符串(string):valid。
如果没有明文声明success为一个function的话,验证成功之后,validate将在你的元素上面添加一个class="valid"的样式。如果声明了success为一个function的话,将和errorPlacement一起作为方法执行。
如果需要success作为function执行的话,可以给一个空的function,如下:
$.validator.setDefaults({ success: function (label) { } });
不一定正确,但是我觉得是这样的。
你可以看看validate的源码里面showLabel这一段代码,我的validate版本是1.12.0,大概在721行的位置。
试试在success成功后 remove掉error的属性
success:function(label){
label.removeClass("xxxxx");
}
删掉success函数就行了
楼主解决了没?我遇到同样的问题,在线等
我也遇到这个问题了。。。有解决的吗?
问题解决了吗,我也是!