2016-03-05 80 views
0

用戶輸入輸入,單擊一個按鈕,然後調用一個函數來執行一些計算。如果某個字段的類型或值不正確,則會顯示錯誤消息。如果其中一個字段錯誤,那麼該字段需要顯示錯誤消息。如果多個字段錯誤,那麼這些字段需要顯示錯誤消息。現在,即使所有字段都是錯誤的,如果字段錯誤,頁面也只顯示一條錯誤消息。我確信這是一個簡單的解決方法,但我一直在撞牆。有什麼建議麼?謝謝!這裏是代碼適用部分:JavaScript多個錯誤消息

if (isNaN(theMilesDriven) || theMilesDriven <= 0) { 
      theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven."; 
      return false; 
     } 

     if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) { 
      theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used."; 
      return false; 
     } 

     if (isNaN(thePriceGallon) || thePriceGallon <= 0) { 
      thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon."; 
      return false; 
     } 
+1

'return'語句立即退出函數。 – Pointy

+0

刪除'return'語句 –

+0

因此,如果我完全刪除了返回語句並將計算放在else語句中,那麼這看起來像是一個有效的修復嗎?謝謝! – MatthewSpire

回答

1

您的解決方案仍然會計算如果前兩個錯誤之一被發現。這裏有一個替代方案:

var isErrorFound = false; 

    if (isNaN(theMilesDriven) || theMilesDriven <= 0) { 
     theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven."; 
     isErrorFound = true; 
    } 

    if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) { 
     theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used."; 
     isErrorFound = true; 
    } 

    if (isNaN(thePriceGallon) || thePriceGallon <= 0) { 
     thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon."; 
     isErrorFound = true; 
    } 

    if (!isErrorFound) { 
     //Perform the calculations and write the results to the page 
     var milesPerGallon = theMilesDriven/theGallonsUsed; 
     var theMPG = document.getElementById("mpg"); 
     theMPG.firstChild.nodeValue = "Your MPG is: " + milesPerGallon.toFixed(2); 

     var cost = theGallonsUsed * thePriceGallon; 
     var theCost = document.getElementById("cost"); 
     theCost.firstChild.nodeValue = "Your cost for this trip is: $" + cost.toFixed(2); 
    } 
+0

謝謝!我想我現在已經明白了。我提出了您的建議更改以及其他一些更改。我感謝您的幫助。 – MatthewSpire

+0

不客氣! – ConnorsFan

0

這裏就是我所做的,似乎要解決的問題:

//Verify the input is numerical and greater than 0 
     if (isNaN(theMilesDriven) || theMilesDriven <= 0) { 
      theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven."; 
     } 

     if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) { 
      theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used."; 
     } 

     if (isNaN(thePriceGallon) || thePriceGallon <= 0) { 
      thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon."; 
     } 

     else { 
      //Perform the calculations and write the results to the page 
      var milesPerGallon = theMilesDriven/theGallonsUsed; 
      var theMPG = document.getElementById("mpg"); 
      theMPG.firstChild.nodeValue = "Your MPG is: " + milesPerGallon.toFixed(2); 

      var cost = theGallonsUsed * thePriceGallon; 
      var theCost = document.getElementById("cost"); 
      theCost.firstChild.nodeValue = "Your cost for this trip is: $" + cost.toFixed(2); 
     } 
+0

這要歸功於其他人的建議,我們非常感謝。 – MatthewSpire