问题
I have jquery validator running and the page is very long, so I want the page to scroll up to the top because I display errors on the very top of the page above the form, does anyone know where I can put the code for the animation so it fires when the form has errors?
回答1:
You can use the invalidHandler option for the exact case you want, like this:
$("form").validate({
rules: { ... rules here ... }
invalidHandler: function(form, validator) {
$('html, body').animate({scrollTop: '0px'}, 300);
}
});
回答2:
The same as @Nick suggested, but to use after plugin initialization:
$("form").bind("invalid-form.validate", function() {
});
Its the same that happens internally when the plugin initializes. Source.
来源:https://stackoverflow.com/questions/2885346/how-to-fire-an-event-on-error-in-jquery-validator-plugin