2012-02-02 46 views
-1

我的應用程序出現問題,它的號碼是here。請按照以下步驟進行:我想用這種方式顯示我的提醒

  1. 當你打開應用程序,點擊「打開網格」和選擇選項4按鈕A,B,C和d下方將出現。

  2. 在Number of Answers文本框中,輸入數字1.然後單擊A,B,C或D中的1個按鈕(所選按鈕將變爲綠色)。當你這樣做後,點擊「添加問題」按鈕兩次。

  3. 正如您所看到的,您在頂部輸入的詳細信息將顯示在下方的2個新行中。在添加的第一個新行中,在「Number of Answers」列下的文本框中,將數字1更改爲0,並在第二行將Answers文本框的數目從1更改爲2.現在單擊「提交詳細信息「按鈕下面。

將出現一個警告,其中指出「您選擇的答案少於所需數量」。並且還說明「你已經選擇了比所需數額更多的答案」。這是正確的,因爲在第一行中,選擇了一個答案,但在文本框中它聲明你想要0個答案,而在第二行中有一個答案選中,但在文本框中它表明你想要2個答案。

我的問題:

我的問題是什麼,雖然,除了顯示在兩行警報兩條消息,我想那是什麼,它會顯示出在第一行第一個錯誤警報。在用戶對第一行中的錯誤進行排序後,如果第二行出現錯誤,那麼當用戶再次單擊「提交詳細信息」按鈕,然後在第二行中顯示警告顯示錯誤消息。

我要顯示警報像這樣的第一行,例如:

請在問題1解決錯誤: 「您選定了所需要的量更少的答案」。

在提醒第二行我想這一點:

請修復錯誤問題2: 「您選定了所需要的量更多的答案」。

下面是相關的驗證代碼(我認爲這個問題是VaR的背景下,我覺得變量是錯的,但我不知道):

function validation() { 

     alertValidation= ""; 
      // Note, this is just so it's declared... 

      $(".numberAnswerTxtRow").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"; 
      } 
     else if (currenttotal > $(this).val()){ 
      alertValidation += "\nYou have selected more answers than the required amount\n"; 
     } 

     else if (currenttotal < $(this).val()) { 
      alertValidation += "\nYou have selected less answers than the required amount\n"; 
     } 
     }); 


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

希望這是有道理的,如果它不那麼請給我評論。

下面是編輯與文本區域驗證驗證()函數:

function validation() { 

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

    alertValidation= ""; 

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

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

    }); 

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

    alertValidation= ""; 

    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"; 
    } 
    else if (currenttotal > $(this).val()){ 
     alertValidation += "\nYou have selected more answers than the required amount\n"; 
    } 

    else if (currenttotal < $(this).val()) { 
     alertValidation += "\nYou have selected less answers than the required amount\n"; 
    } 

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

    return true; 
} 

回答

0

你應該返回false一次,如果任何錯誤條件是有效的阻止每一個循環。嘗試這個。

function validation() { 

     var alertValidation = ""; 
     // Note, this is just so it's declared... 

     $(".numberAnswerTxtRow").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"; 
      } 
      else if (currenttotal > $(this).val()){ 
       alertValidation += "\nYou have selected more answers than the required amount\n"; 
      } 

      else if (currenttotal < $(this).val()) { 
       alertValidation += "\nYou have selected less answers than the required amount\n"; 
      } 
      if(alertValidation != ""){ 
       return false;//Stop the each loop 
      } 
     }); 


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

對不起,我沒有看到這個答案。我已經測試過這個,這似乎是一個非常好的解決方案。最好的答案:) – user1180490 2012-02-02 19:41:58

0

把你的if檢查放在循環中。如果第一行發現錯誤,應該會發出警報並返回。如果沒有,它會繼續檢查第二行,如果發現錯誤並返回。

function validation() { 

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

     alertValidation= ""; 

     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"; 
     } 
     else if (currenttotal > $(this).val()){ 
      alertValidation += "\nYou have selected more answers than the required amount\n"; 
     } 

     else if (currenttotal < $(this).val()) { 
      alertValidation += "\nYou have selected less answers than the required amount\n"; 
     } 

     if(alertValidation != "") 
     { 
      alert(alertValidation); 
      //Ends the loop 
      return false; 
     } 
    }); 

//Do other stuff here... 
} 
+0

如果我這樣做你的方式,那麼我只是想知道,我要爲每行添加一個textarea,如果textarea是空的,那麼這樣做:'$(「。textAreaQuestion」 ).each(function(){'最好還是將它放在哪裏? – user1180490 2012-02-02 19:02:25

+0

這會提醒並返回每個循環,返回到你的主函數驗證(){}任何你想做的事情都可以 – 2012-02-02 19:08:38

+0

我編輯了我的答案,以更好地展示發生了什麼 – 2012-02-02 19:11:53