1

我在用

   $("#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);
            }
        }
 });

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

如果您需要更多信息,请告诉我。

4

1 回答 1

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"
    },
});
于 2013-05-22T16:11:32.973 回答