我已经为jquery的验证器插件创建了一个方法,它的工作方式类似于远程规则。不同之处在于,我希望显示一条动态错误消息(基于ajax响应)。
jQuery.validator.addMethod("duplicate", function(value, element, params) {
var object_settings = this.settings;
params.data[$(element).attr("name")] = value;
$.post(params.url, params.data, function(response) {
if (response == 'true'){ return true; }
else {
object_settings.messages[element.name] = response;
return false;
}
}, 'text');
}, '');
It works...sort of....it设置消息,但最初并不显示它(如果再次验证该字段,则会显示该消息)。
有什么建议吗?
(也许远程规则提供了此功能……我在文档中找不到任何东西)
发布于 2009-06-10 17:40:11
下面是调用对象的showErrors函数的solution....needed:
jQuery.validator.addMethod("duplicate", function(value, element, params) {
var validator = this;
params.data[element.name] = value;
$.post(params.url, params.data, function(response) {
if (response == 'true'){ return true; }
else {
var errors = {};
errors[element.name] = response;
validator.showErrors(errors);
return false;
}
}, 'text');
}, '');
取自jquery.validate.js中的"remote“( 917 -919行)
发布于 2011-01-11 12:08:48
也在寻找这个问题的解决方案,发现了这个...
在原始示例中,如果更改此行:
object_settings.messages[element.name] = response;
要这样做:
$.validator.messages.duplicate = response;
这对我很有效。我在这里找到的:http://blogs.teamb.com/craigstuntz/2009/01/15/37923/
发布于 2013-11-27 18:44:10
我遵循了网站http://blogs.teamb.com/craigstuntz/2009/01/15/37923/#comment-125774中提到的过程,并成功了。
您必须使用动态消息调用该方法,以便它将显示该消息。例如
$.validator.addMethod("validatePremium", function(value, element, param) {
if( Condition )
{
$.validator.messages.validatePremium = "your message here";
//enter code here
//...
return false;
}
}, $.validator.messages.validatePremium);
https://stackoverflow.com/questions/976384
复制相似问题