2016-03-05 112 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