2012-02-04 91 views
0

在此jsfiddle的方式,請按照下列步驟操作:警報不工作,我希望它的工作

1:當您在「添加問題」按鈕,打開小提琴點擊兩次,這將下面添加2行。

2:點擊「提交信息」按鈕,它會想出這個錯誤:

You have errors on Question Number: 2

You have not entered a valid Question

Please Enter in the Number of Answers you Require for this question

Please enter in a figure for Number of Marks for this Question

You have not entered a valid Question

Please Enter in the Number of Answers you Require for this question

Please enter in a figure for Number of Marks for this Question

這是不正確,上面顯示的警告所有的錯誤和不正確的問題數。應該發生的情況是,如果多行存在錯誤,則應顯示包含錯誤的第一行的編號並在警報中顯示這些錯誤。警報應顯示在此:

You have errors on Question Number: 1

You have not entered a valid Question

Please Enter in the Number of Answers you Require for this question

Please enter in a figure for Number of Marks for this Question

而且另一個問題是,如果在第二行,你在文字區域和文本框填寫,它會顯示下面這個警告:

You have errors on Question Number: 2

You have not entered a valid Question

Please Enter in the Number of Answers you Require for this question

Please enter in a figure for Number of Marks for this Question

上述警告是不正確的它指出錯誤的錯誤問題編號。問題編號應該是數字1,而不是數字2.那麼,如何解決這個問題。

下面是其中的警報控制的驗證()函數的代碼:

function validation() { 

    var _qid = ""; 
    var _msg = ""; 

    alertValidation = ""; 
    // Note, this is just so it's declared... 
    $("tr.optionAndAnswer").each(function() { 


     _qid = $("td.qid",this).text(); 
     _msg = "You have errors on Question Number: " + _qid + "\n"; 

     $(".textAreaQuestion",this).each(function() { 



      if (!this.value || this.value.length < 5) { 
       alertValidation += "\nYou have not entered a valid Question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 

     $(".numberAnswerTxtRow",this).each(function() { 


      var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length; 

      if (!this.value) { 
       alertValidation += "\nPlease Enter in the Number of Answers you Require for this question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 

     }); 

     $(".txtWeightRow",this).each(function() { 


      if (!this.value) { 
       alertValidation += "\nPlease enter in a figure for Number of Marks for this Question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 
    }); 

    if (alertValidation != "") { 
     alert(_msg + alertValidation); 
     return false; 
    } 

    return true; 
} 

回答

1

你必須檢查是否在外環($("tr.optionAndAnswer").each())已經發現任何錯誤,通過返回false停止。

在每個外部循環中添加此項,並聲明alertValidationvar alertValidation否則它將位於全局範圍內。

if(alertValidation != ""){ 
     return false; 
    } 

我已經修復它在你的小提琴演示看看。

http://jsfiddle.net/ShankarSangoli/euL5e/1/

+0

謝謝你,這已經非常完美 – user1182476 2012-02-04 20:41:08

1

我想你剛剛退出的$("tr.optionAndAnswer").each萬一alertValidation有內容(可以更換,如果(alertValidation != "")if (alertValidation))。

看一看http://jsfiddle.net/GgvEs/

+0

Upvot的答案,這個答案也是正確的,謝謝 – user1182476 2012-02-04 20:40:41