【问题标题】:Jquery validation plugin errorplacement for single element单个元素的 Jquery 验证插件错误放置
【发布时间】:2015-03-04 10:58:07
【问题描述】:

我正在使用 Jquery 验证插件来验证表单。当验证表单的一个元素对齐不正确时。

如果您看到图像,验证表单时城市字段icon + 按钮对齐不正确。因为标签错误验证显示在输入元素和 icon + 之间。我需要在元素下方显示错误消息。

我的城市字段的html代码是这样的

    <tr>
        <td align="right"><span class="mandetry">*</span> City:</td>
         <td>
       <div class="input-group" id="app_details">
      <input type="text" class="form-control client_city" name="client_city" id="city_name" value="<?php echo set_value('client_city')?>"> 
     <span class="input-group-btn">
     <a class="btn btn-default" id='addnewcity' href="<?php echo base_url('addnewcity')?>"><i class="fa fa-plus-square"></i></a>
     </span>
   <div id="messageBox"></div> <!-- Here i would like to display message-->
    </div> </tr>

js代码是这样的

$(document).ready(function(){
$('#add_client').validate({
    errorClass: 'validation_errors',
    debug: false,
     rules: {
         client_name:{required:true},
         client_address:{required:true},
         client_city:{required:true},
         errorPlacement: function(error, element) {
                if (element.attr("name") == "client_city" )
                {
                    error.appendTo("#messageBox");
                }
            }
     },
     messages: {
         client_name:{required:"The Client name is a required / mandatory field"},
         client_address:{required:"The Client address is a required / mandatory field"},
         client_city:{required:"The City is a required / mandatory field"},

     }

});
     });

messageBox div没有附加错误信息。js中的errorPlacement有什么问题吗?只有 city 元素我需要正确显示错误消息。对于其他表单字段,它不应该改变。我无法解决这个问题。请给我建议。谢谢。

【问题讨论】:

    标签: javascript jquery html css jquery-validate


    【解决方案1】:

    你缺少else部分,如果不是client_city元素那么你需要在

    之后插入错误
    $(document).ready(function () {
        $('#add_client').validate({
            errorClass: 'validation_errors',
            debug: false,
            rules: {
                client_name: {
                    required: true
                },
                client_address: {
                    required: true
                },
                client_city: {
                    required: true
                }
            },
            errorPlacement: function (error, element) {
                console.log('dd', element.attr("name"))
                if (element.attr("name") == "client_city") {
                    error.appendTo("#messageBox");
                } else {
                    error.insertAfter(element)
                }
            },
            messages: {
                client_name: {
                    required: "The Client name is a required / mandatory field"
                },
                client_address: {
                    required: "The Client address is a required / mandatory field"
                },
                client_city: {
                    required: "The City is a required / mandatory field"
                },
    
            }
    
        });
    });
    

    演示:Fiddle

    【讨论】:

    • 我添加了else 条件。还是没有结果。如果我检查字段之间显示的 html 消息
    • 如果我使用errorLabelContainer: "#messageBox" 附加到messageBox 的所有元素验证消息。但我不想那样。
    • 您将errorPlacement 放在rules 对象中,这是错误的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多