Jquery Validation plug-in custom error placement

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

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

4条回答
  •  失恋的感觉
    2020-11-28 08:32

    I'm using the metadata extension with the validator.. (note, I'm setting it to use the data-meta attribute on the markup...)

    then in code...

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

    I've been using the metadata for a lot of similar functionality, which works rather nicely... note, I used the single ticks (apostrophes) around the meta data, this way you can use a JSON serializer server-side to inject into that portion of the tag (which should use double-quotes around strings)... a literal apos may be an issue though, (replace "'" with "\x27" in the string).

提交回复
热议问题