2013-04-16 34 views
-1

我遇到了類似的問題。不應該「錯誤」是一個錯誤字符串?當我提醒錯誤時它是一個對象([對象對象]),當我把「errorPlacement」塊取出時,它工作正常,我試着把errorPlacement塊放在最後,當我改變這樣的第6行: element .closest(「div」)。append(「test message」); I do GET「test message」string in the form without error。所以問題是「error」是一個對象而不是字符串。問題的插件 我使用jQuery驗證插件1.7和jQuery 1.6ishJQuery驗證插件errorPlacement在需要字符串時傳遞錯誤對象

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

是的,'錯誤'應該是一個對象。現在有什麼問題,你真的想做什麼? – Sparky

回答

1

報價OP:

「不應該 「錯誤」 是一個錯誤的字符串時?我警告錯誤它是一個 對象([object Object])。「

不,它不應該是一個字符串。這的確是一個對象,它看起來是這樣的......

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

如果你想提取從error對象的字符串,使用jQuery .text() method

這會給你一個包含錯誤消息的字符串:

error.text() 

否則,目前還不清楚你與你的代碼有什麼問題。

相關問題