How to fire an event on error in jquery validator plugin

孤街浪徒 提交于 2019-12-22 03:52:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!