-1

我有一个类似的问题。“错误”不应该是错误字符串吗?当我警告错误时,它是一个对象([object Object]。当我取出“errorPlacement”块时它工作正常。我也尝试将errorPlacement块放在最后。当我像这样更改第6行时:元素。最接近(“div”).append(“测试消息”);我确实在表单中得到“测试消息”字符串而没有错误。所以问题是“错误”是一个对象而不是一个字符串。这是一个问题插件?我正在使用 jQuery 验证插件 1.7 和 jquery 1.6ish

$("#student-set-form").validate({
        errorPlacement: function(error, element) {                
            console.log(error);
            alert(error);
            element.closest("div").append(error);
        },
        rules: {
            NAME: {
                required: true
            },
            LOCATION: {
                required: true
            },
            DESC: {
            },
            MIN_GPA: {
                required: function() {
                    return $("input[name='MAX_GPA']").val() != ''; // yes, min references max
                },
                range: [0, 4]
            },
            MAX_GPA: {
                required: function() {
                    return $("input[name='MIN_GPA']").val() != ''; // yes, max references min   
                },
                range: [0, 4]
            },
            MIN_CR_HOURS_COMPLETE: {
                required: function() {
                    return $("input[name='MAX_CR_HOURS_COMPLETE']").val() != ''; // yes, min references max  
                },
                range: [0, 200]
            },
            MAX_CR_HOURS_COMPLETE: {
                required: function() {
                    return $("input[name='MIN_CR_HOURS_COMPLETE']").val() != ''; // yes, max references min           
                },
                range: [0, 200]
            },
            MIN_LASTNAME_INITIAL: {
                required: function() {
                    return $("input[name='MAX_LASTNAME_INITIAL']").val() != ''; // yes, max references min           
                },
            },
            MAX_LASTNAME_INITIAL: {
                required: function() {
                    return $("input[name='MIN_LASTNAME_INITIAL']").val() != ''; // yes, max references min           
                },
            },
            DAYS_VISIBLE: {
                required: true,
                range: [1, 365]
            },
            DAYS_BEFORE_VISIBLE: {
                required: true,
                range: [1, 3]
            },
            APPOINTMENT_LEGNTH_MINUTES: {
                required: true,
                range: [1, 40320]
            },
            NUM_STUDENTS: {
                required: true,
                range: [1, 10000]
            }

        },
        messages: {
            NAME: {
                required: "Please enter a name."
            },
            LOCATION: {
                required: "Please enter a location."
            },
            DESC: {
            },
            MIN_GPA: {
                required: "Min/Max GPA are optional, but if one is set, the other must also be set.",
                range: "Min GPA must be between 0 and 4.0"
            },
            MAX_GPA: {
                required: "Min/Max GPA are optional, but if one is set, the other must also be set.",
                range: "Man GPA must be between 0 and 4.0"
            },
            MIN_CR_HOURS_COMPLETE: {
                required: "Min/Max Credit hours are optional, but if one is set, the other must also be set.",
                range: "Min GPA must be between 0 and 4.0"
            },
            MAX_CR_HOURS_COMPLETE: {
                required: "Min/Max Credit hours are optional, but if one is set, the other must also be set.",
                range: "Max Cr Hrs must be between 0 and 200."
            },
            MIN_LASTNAME_INITIAL: {
                required: "Min/Max Last Initials are optional, but if one is set, the other must also be set."
            },
            MAX_LASTNAME_INITIAL: {
                required: "Min/Max Last Initials are optional, but if one is set, the other must also be set."
            },
            DAYS_VISIBLE: {
                required: "Number days prior visible is required.",
                range: "Number days prior visible must be between 1 and 365 days."
            },
            DAYS_BEFORE_VISIBLE: {
                required: "# weekdays hide before appt is required.",
                range: "# weekdays hide before appt must be be between 1, 2, or 3 days."
            },
            APPOINTMENT_LEGNTH_MINUTES: {
                required: "Appointment Length in minutes is required.",
                range: "Appointment Length in minutes must be beween 1 and 1000 minutes."
            }
        }
    });
4

1 回答 1

2

引用操作:

““错误”不应该是错误字符串吗?当我警告错误时,它是一个对象([对象对象]。”

不,它不应该是一个字符串。它确实是一个看起来像这样的物体......

<label for="myfield" class="error">This field is required</label>

如果要从error对象中提取字符串,请使用jQuery.text()方法

这将为您提供一个包含错误消息的字符串:

error.text()

否则,不清楚您的代码有什么问题。

于 2013-04-16T20:42:40.187 回答