-1

私は同様の問題を抱えています。「エラー」はエラー文字列であるべきではありませんか? エラーを警告すると、それはobject([object Object]です。「errorPlacement」ブロックを取り出すと、正常に動作します。最後にerrorPlacementブロックも入れてみました。6行目を次のように変更すると、要素. closest("div").append("test message"); I DO GET "test message" string in the form without error. したがって、問題は、"error" が文字列ではなくオブジェクトであることです。これは問題ですか? 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

引用OP:

「「エラー」はエラー文字列であるべきではありませんか?エラーを警告すると、それはオブジェクト([オブジェクトオブジェクト]です。」

いいえ、文字列であってはなりません。それは確かにこのように見えるオブジェクトです...

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

errorオブジェクトから文字列を抽出する場合は、 jQuery.text()メソッドを使用します。

これにより、エラー メッセージを含む文字列が得られます。

error.text()

そうしないと、コードでどのような問題が発生しているかが明確になりません。

于 2013-04-16T20:42:40.187 に答える