Using the jQuery Validate plugin, how can I wrap the error message string in a span

前端 未结 3 1352
再見小時候
再見小時候 2021-01-26 13:15

Does anyone know how to wrap the inner error string in a span? I\'m using the jQuery Validate plugin for my form and getting the default error message displaying in label tags,

相关标签:
3条回答
  • 2021-01-26 13:31

    you can define your error message like:

    var validator = form.validate({
      rules: {
        username: {
          required: true,
          minlength: 3
        }
      },
      messages: {
        username: {
          required: '<span> required </span>',
          minlength: ''
        }
      },
    
    0 讨论(0)
  • 2021-01-26 13:39

    Use the errorElement option to wrap each item in a span.

    Then use the wrapper option to enclose each span within a label

    $('#myform').validate({
        errorElement: 'span',
        wrapper: 'label'
        ....
    

    Will give you...

    <label><span class="error">This is Required</span></label>
    

    For CSS & jQuery targeting, you don't need a special class as you can select like this...

    label > span
    

    OR more specifically...

    label > span.error
    

    DEMO: http://jsfiddle.net/s89ommhx/

    0 讨论(0)
  • 2021-01-26 13:40

    Use wrapInner .

    $('.error').wrapInner( "<span class='youreAwesome'></span>");
    

    As per op comment :

    $(".selector").validate({
      invalidHandler: function(event, validator) {
      $('.error').wrapInner( "<span class='youreAwesome'></span>"); 
       }
    });
    

    Fiddle

    0 讨论(0)
提交回复
热议问题