这有效:
$("[id$='_zzz']").rules(
"add",
{
required: true,
minlength: 8,
messages: {
required: "...",
minlength: jQuery.format("...")
}
}
);
出现错误消息。
当我尝试设置消息样式时,这不起作用:
$("[id$='_zzz']").rules(
"add",
{
required: true,
minlength: 8,
messages: {
required: "...",
minlength: jQuery.format("...")
},
errorElement: "span",
errorPlacement: function(error, element) {
error.insertAfter(element);
error.css("margin", "0 0 0 5px");
}
}
);
当我通过验证函数设置样式时,样式会被应用,因此它可以工作:
$('#aspnetForm').validate({
errorElement: "span",
errorPlacement: function(error, element) {
error.insertAfter(element);
error.css("margin", "0 0 0 5px");
}
});
为什么我不能在规则添加功能中使用 errorplacement 进行样式设置?
最佳答案
在第一种方法中,您将对象上的 errorPlacement
属性/函数传递给规则方法...它根本不关心对此的检查,也不使用它。对于传递给 JavaScript 的任何对象,它都必须检查该属性或以某种方式使用它,否则它只是无关的信息。您也可以将 myExtrathing:'ValueHere'
添加到对象中,不会破坏任何内容...但也不会被使用。
.validate()
method has an existing errorPlacement
function (即积极使用,这是重要的部分,它正在寻找它)在默认选项和 performs an extend 中将默认方法替换为您提供的方法...如果您提供了一种方法。对于几乎每个 jQuery 插件和您指定的任何选项来说,同样的行为都是如此,它将默认选项与您提供/覆盖的选项合并。您可以看到此行为的一些简单示例 in the jQuery plugin authoring manual .
关于jQuery 验证错误放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699152/