How to focus invalid fields with jQuery validate?

后端 未结 2 1061
耶瑟儿~
耶瑟儿~ 2021-01-30 10:13

There is focusInvalid option, which is true by default. But it works only when form submission happens. If I validate the form with valid method, then it doesn\'t w

2条回答
  •  一向
    一向 (楼主)
    2021-01-30 11:11

    With the invalidHandler you can set focus to the first element that fails:

    $("#form").validate({
        onfocusout: false,
        invalidHandler: function(form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {                    
                validator.errorList[0].element.focus();
            }
        } 
    });
    

提交回复
热议问题