Jquery Validation plug-in custom error placement

后端 未结 4 677
终归单人心
终归单人心 2020-11-28 08:26

Using the jQuery Validation plug-in for the following form:

4条回答
  •  感动是毒
    2020-11-28 08:41

    This is a basic structure, you can use whatever selector you would like in the method. You have the error element and the element that was invalid.

    jQuery.validator.setDefaults({
        errorPlacement: function(error, element) {
            error.appendTo(element.prev());
        }
    });
    

    Or to target the ID, you could do

    jQuery.validator.setDefaults({
        errorPlacement: function(error, element) {
            error.appendTo('#invalid-' + element.attr('id'));
        }
    });
    

    Not tested, but should work.

提交回复
热议问题