JavaScript多个错误消息

JavaScript多个错误消息,javascript,validation,dom,Javascript,Validation,Dom,用户输入输入,单击一个按钮,然后调用一个函数来执行一些计算。如果字段的类型或值不正确,则会显示错误消息。如果其中一个字段错误,则该字段需要显示错误消息。如果多个字段错误,则这些字段需要显示错误消息。现在,如果某个字段错误,即使所有字段都错误,页面也只显示其中一条错误消息。我相信这是一个很容易解决的问题,但我总是把头撞在墙上。有什么建议吗?非常感谢。以下是代码的适用部分: if (isNaN(theMilesDriven) || theMilesDriven <= 0) {

用户输入输入,单击一个按钮,然后调用一个函数来执行一些计算。如果字段的类型或值不正确,则会显示错误消息。如果其中一个字段错误,则该字段需要显示错误消息。如果多个字段错误,则这些字段需要显示错误消息。现在,如果某个字段错误,即使所有字段都错误,页面也只显示其中一条错误消息。我相信这是一个很容易解决的问题,但我总是把头撞在墙上。有什么建议吗?非常感谢。以下是代码的适用部分:

    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;
        }

if(isNaN(theMilesDriven)| | theMilesDriven以下是我所做的,似乎解决了问题:

    //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

如果(isNaN(theMilesDriven)| | theMilesDriven发现前两个错误中的一个,则您的解决方案仍将进行计算。以下是一个备选方案:

    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);
    }
var-isErrorFound=false;

如果(isNaN(邮件驱动)| |文件驱动
return
语句立即退出函数。remove
return
语句因此,如果我完全删除return语句并将计算放在else语句中,这似乎是一个有效的修复方法吗?谢谢!这是由于其他人的建议而完成的,非常感谢。谢谢!我想我现在明白了。我做了你建议的改变以及其他一些。我感谢你的帮助。