javascript - 单个元素的 jquery 验证插件 errorplacement

标签 javascript jquery html css jquery-validate

我正在使用 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 元素我需要正确显示错误消息。对于其他表单字段,它不应该改变。我无法解决这个问题。请建议我。谢谢。

最佳答案

您缺少 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

关于javascript - 单个元素的 jquery 验证插件 errorplacement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28852704/

相关文章:

javascript - 如何将 Raphael.js 集移动到特定位置?

javascript - Jest 'projects' 配置不读取子文件夹中的 .babelrc 和 webpack.config.json

javascript - Bootstrap Datepicker 防止日期格式更改 + 结束日期晚于开始日期

javascript - current_page_item 无法在 wordpress 中使用静态菜单

javascript - 如何在JS中使用重音字符等特殊字符?

javascript - D3.js - 在 Y 轴上显示刻度值

javascript - DIV 中支持 jQueryMobile 的站点

php - ?action=register URL 上的 WordPress 垃圾邮件

javascript - 隐藏/显示 div 最重要的是回来

javascript - 如何在 Vue 3 中导入 svg?