问题描述
我无法将回调附加到jquery unobtrusive validate。我想要做的是在显示字段验证错误时隐藏信息性消息。它们都占据了页面上的相同区域。我无法弄清楚如何将回调附加到show error方法。我有一个可以工作,如果你调用验证,但错误消息显示之前甚至更改为该字段被触发。
I'm having trouble attaching a call back to jquery unobtrusive validate. What I want to do is hide an informational message any time a field validation error is shown. They both take up the same area on the page. I can't figure out how to attach a callback to the show error method. I have one that works if you call validate but the error messages are shown before even change for the field is fired.
这是一个jsfiddle演示我的问题。
Here is a jsfiddle demoing my problem. http://jsfiddle.net/K6NcF/4/
这是仅在调用validate()时运行的回调。
This is the callback that only runs when validate() is called.
$('#theForm').bind('invalid-form.validate',function(){
$('.info-box').hide();
});
任何帮助都将不胜感激。
Any help would be appreciated.
推荐答案
当使用不引人注目的时候,您可以访问突出显示和取消强调回调
You can access the highlight and unhighlight callbacks when using unobtrusive like this
// grab a reference to the validator instance
var v = $('form').validate();
// reference the settings property, and the highlight function
var originalHighlight = v.settings.highlight;
你可以用你的东西覆盖它,或者下面的例子显示了添加自己功能的方法没有覆盖那里的东西。
You could just overwrite this with your stuff, or the example below shows a way to add your own functionality without overwriting what is there.
var v = $('#theForm').validate();
var originalHighlight = v.settings.highlight;
var originalUnHighlight = v.settings.unhighlight;
v.settings.highlight = function(element, errorClass, validClass)
{
console.log('doing my stuff here');
$('.info-box').hide();
originalHighlight.call(v, element, errorClass, validClass);
}
v.settings.unhighlight = function(element, errorClass, validClass)
{
console.log('undoing my stuff here');
$('.info-box').show();
originalUnHighlight.call(v, element, errorClass, validClass);
}
在这里摆弄
这篇关于麻烦将回调附加到不显眼的验证显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!