Open accordion panel on validation error

前端 未结 3 1587
迷失自我
迷失自我 2021-02-10 09:38

I am using jQuery accordion to split my forms into several panels and jQquery validation to check the required fields. It works great to show errors in validated fields as long

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-10 10:13

    The API has changed slightly, here is a working example based on the selected answer for anyone in the future. Note the ignore which is needed to prevent the hidden fields in an accordion from being overlooked by the validator.

    $('form').validate({
        invalidHandler: function(event, validator) {
            if (validator.numberOfInvalids() > 0) {
                validator.showErrors();
                // Open accordion tab with errors
                var index = $(".has-error")
                    .closest(".ui-accordion-content")
                    .index(".ui-accordion-content");
                $(".accordion").accordion("option", "active", index);
            }
        },
        ignore: [],
    });
    

提交回复
热议问题