当需要字符串时,JQuery验证插件errorPlacement会传入错误对象

时间:2013-04-16 18:20:51

标签: jquery jquery-validate

我遇到了类似的问题。不应该“错误”是一个错误字符串?当我发出错误警告时,它是一个对象([object Object]。当我取出“errorPlacement”块时,它工作正常。我尝试将errorPlacement块放在最后。当我像这样更改第6行时: element.closest(“div”)。append(“test message”); 我在表单中获取“测试消息”字符串,没有错误。所以问题是“错误”是一个对象而不是一个字符串。这是插件的问题吗? 我正在使用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."
            }
        }
    });

1 个答案:

答案 0 :(得分:2)

引用OP:

  

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

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

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

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

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

error.text()

否则,您不清楚您的代码遇到了什么问题。