【问题标题】:It seems my class is being given both my error and success class when it should only be given the success class似乎我的班级同时获得了我的错误和成功课程,而它应该只获得成功课程
【发布时间】:2019-04-17 10:35:18
【问题描述】:

我有一个想要验证的表单,我在我的 input 框和需要的文本框周围放置了 div,但是当我在框中输入内容并提交时,它不会提交,因为还有其他未选择的东西,但它给了我的 div 错误和成功类,而它应该只是成功......

jquery 验证码:

$("#frm_enter").validate({
    ignore: 'hidden:not(input[name=\'terms,privacy,iam18\'])',
    onkeyup: false,
    errorElement: "div",
    rules: {
        code:{required: true,},
        terms:{required: true,},
        privacy:{required: true},
        iam18:{required: true}
    },
    messages: {
        code:{required: "Code is required"},
        terms:{required: "Please accept terms and conditions"},
        privacy:{required: "Please accept privacy notice"},
        iam18:{required: "Please verify you are 18 or older"}
    },
    errorPlacement: function(error, element) {
        error.appendTo( '#v_' + element.attr('id') + '.frmvalidate' );
          $('#v_' + element.attr('id') + '.frmvalidate').parent().addClass("error1");
         console.log("is this working");
    },
    success: function(label) {
        $("#submit").prop('disabled', false);
        console.log(label);
         $(label).parent().parent().addClass("valid1")
    }
});

HTML:

<div class = "input_validate">                                  
<div class="board_question">
Code :                                  
</div>                          
<div class="board_input">                               
<input type="text" maxlength="11" id="code" name="code" />                          
</div>                              
<p id="v_code" class="frmvalidate"></p>                             
</div>

我希望它能给我的input_validate 班级提供valid1 的班级,但它同时给了我valid1error1

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:

    您错误地使用了此插件的选项。您不应该使用errorPlacementsuccess 来添加/删除任何类。这两个函数不会在您期望的时候触发。 errorPlacement 仅触发将错误放置在布局中,然后插件在验证期间切换标签的可见性。 success 在元素完全有效时触发,但其预期目的是在元素有效时利用验证消息标签,而不是简单地隐藏它。

    为了在验证期间自动添加/删除有效/无效类,您应该只使用highlightunhighlight 函数。

    I suggest you read the full documentation.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      相关资源
      最近更新 更多