2012-12-04 36 views
1

我正在使用jQuery驗證插件爲我的網站中的某些表單。但是其中一種形式的驗證不適用於IE7和8,但其他形式使用相同的驗證腳本。我試圖調試,發現問題出在showErrors部分,當我通過使用「for in」JS循環errorMap時,show object不支持錯誤。任何想法如何得到修復?JS「for in」error IE7/8

下面是HTML我有

<form name="donate" id="form1" action="https://www.xxxx.xxx.xx/api/help" method="POST" target="_top">  
      <input type="hidden" name="face" id="faceinput" style="clear: both; margin:0; padding:0;"> 
      <input type="hidden" name="image" id="imageinput" style="clear: both; margin:0; padding:0;"> 
      <h3>Thank you for helping! </h3> 
      <h4>Please fill out the following details.</h4>  
      <label>Name: </label> 
      <input type="text" name="fullname" id="fullname" class="required" /> 
      <div class="clearfix"></div> 
      <label>Email: </label> 
      <input type="text" name="email" id="email" class="required email" /> 
      <div class="clearfix"></div> 
      <label>Amount: </label> 
      <input type="text" name="amount" id="amount" class="required number minStrict" /><input type="button" id="submitBtn" value="Donate" /> 
      <div class="clearfix"></div> 
      <div id="error" style="display:none;"> 
       Oops! The minimum amount to donate is $6. Don't wish to donate? Please click on the close button on the top right hand corner instead. 
      </div> 
     </form> 

的JavaScript

$('#form1').validate({ 
       onkeyup: false, 
       focusInvalid: false, 
       rules : { 
        amount : { 
         required : true, 
         number  : true, 
         minStrict : 5 
        } 
       }, 
       messages : { 
        fullname: "Please fill out your name", 
        email: {required:"Please fill out your email address",email:"Invalid email address"}, 
        amount: {required:"Please fill out amount to donate",number:"Invalid amount",minStrict:"Minimum amount is 6$"} 
       }, 
       showErrors: function(errorMap, errorList) { 
        alert('here'); 

        for(error in errorMap){ 
         alert(error); 
         /*if(error=="fullname" || error=="email" || error=="amount"){ 
          //if(errorMap[error]=='Minimum amount is 6$') { 
           //$('div#error').show(); 
          //} 
          //else { 
           $(':text[name='+error+']').val(errorMap[error]).addClass('error');//append err msg in all text field and hightlight 
           $('div#error').hide(); 
          } 
         }*/ 
        } 
       } 
      }); 

$('#submitBtn').click(function(){ 
      if($("#form1").valid()){ 
       alert('here'); 
       $('div#error').hide(); 
       $('#form1').submit(); 
      } 
     }); 

錯誤

SCRIPT438:對象不支持此屬性或方法 validate.js,行26字符5

通過添加var error來修正...

的(在errorMap VAR誤差){//

+0

哪一行是第26行? –

+0

它的for(在errorMap中的錯誤) – zaw

+2

這不會是問題,但你可能想使用'for(var errorMapper){'這樣'error'不會暴露全局 – Ian

回答

1

我不知道爲什麼,但使用

for(var error in errorMap){ 

似乎張貼在評論解決您的問題...

+0

對於爲什麼在全局範圍內使用'error'會引起問題嚴重好奇?如果在全局範圍內定義'var error;'然後使用'for(errorMap中的錯誤){'。 – Sphvn

+0

我在這裏假設'error'被定義爲這個範圍內的jQuery功能的一部分。因此返回的錯誤'對象不支持這個屬性或方法'。這解釋了它! – Sphvn

+0

@Lavabeams嗯,我認爲問題是如果你在聲明一個變量時不使用'var',它會在全局範圍內創建它。因此,因爲技術上循環聲明一個局部變量(以任何方式),不使用'var'這樣做。所以你真的不需要在全局範圍內聲明'error',就好像'for'循環應該沒問題。如果'error'甚至在這個範圍內被jQuery定義,循環應該覆蓋它。但是jQuery不能在不傳遞參數的情況下神奇地填充範圍(OP將其定義爲'errorMap'和'errorList' ...無'錯誤') – Ian