错误位置jquery不显示自定义消息

时间:2013-05-22 15:56:54

标签: jquery validation

我正在使用

   $("#myForm").validate({
     errorLabelContainer: "#messageBox",
        rules: {
            name: "required",
            checkedUserId: {
                required: true
            },
            url: {
               url: true
            },
            messages: { 
                name: "Please enter name ." ,
                checkedUserId: "Please select at least one user"
            },
            errorElement: "div",
            wrapper: "div",
            errorPlacement: function(error, element) {
                offset = element.offset();
                error.insertBefore(element)
                error.addClass('message');  // add a class to the wrapper
                error.css('position', 'absolute');
                error.css('left', offset.left + element.outerWidth());
                error.css('top', offset.top);
            }
        }
 });

它显示我的默认消息“此字段是必需的。”而不是消息我想显示“请输入名称。”

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:2)

您的属性格式不正确,应该是:

rules: {

},
messages: {

}

尝试以下代码,它将起作用(在JSFiddle中测试):

$("#myForm").validate({
    errorLabelContainer: "#messageBox",
    rules: {
        name: "required",
        checkedUserId: {
            required: true
        },
        url: {
            url: true
        },
        errorElement: "div",
        wrapper: "div",
        errorPlacement: function (error, element) {
            offset = element.offset();
            error.insertBefore(element)
            error.addClass('message'); // add a class to the wrapper
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);
        }
    },
    messages: {
        name: "Please enter name .",
        checkedUserId: "Please select at least one user"
    },
});