问题
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.
Here is a jsfiddle demoing my problem. http://jsfiddle.net/K6NcF/4/
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.
回答1:
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);
}
fiddle here http://jsfiddle.net/K6NcF/8/
来源:https://stackoverflow.com/questions/16196633/trouble-attaching-call-back-to-unobtrusive-validation-show-error