我正在使用 jQuery 验证插件,它效果很好。
每当远程ajax失败时,我希望能够显示消息并触发模式(示例中的alert())。我不知道如何做到这两点。现在,它按预期触发了alert(),但还附加了错误消息“请修复此字段”,这应该是我自己的自定义错误消息。
这是我得到的:
$("#adresse-form").validate({
errorElement: "span",
rules: {
navn: {
required: true,
minlength: 5,
maxlength: 25
},
tlf: {
required: true,
digits: true,
minlength: 8,
remote: {
url: "/ajax/check_tlf",
type: "post"
}
}
},
messages: {
navn: "Field Name is required",
tlf: {
required: "Field tlf is required!",
remote: function () { // i want to add the message aswell, not just the alert
alert("failed - tlf is already taken!");
}
}
},
submitHandler: function(form) {
doSomethingGreatOnSuccess();
},
errorPlacement: function (error, element) {
error.appendTo(element.parent());
}
});
最佳答案
Quote OP comment: "The returned string works everytime, but the
.reveal()
only fires the first time, after the page is loaded."
我认为您只获得一次模式,因为验证插件仅构造一次消息(然后使用隐藏/显示)。如果您希望每次都触发它,请尝试使用 highlight
回调函数。与设置为 false
的 onkeyup
和 onfocusout
结合使用。
onkeyup: false,
onfocusout: false,
highlight: function(element, errorClass) {
if ($(element).attr('name') === 'remotefieldname') {
$('#myModal').reveal();
}
}
关于jquery - 当 jQuery 验证远程失败时打开模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773262/