用户输入输入,单击一个按钮,然后调用一个函数来执行一些计算。如果某个字段的类型或值不正确,则会显示错误消息。如果其中一个字段错误,那么该字段需要显示错误消息。如果多个字段错误,那么这些字段需要显示错误消息。现在,即使所有字段都是错误的,如果字段错误,页面也只显示一条错误消息。我确信这是一个简单的解决方法,但我一直在撞墙。有什么建议么?谢谢!这里是代码适用部分: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;
}
'return'语句立即退出函数。 – Pointy
删除'return'语句 –
因此,如果我完全删除了返回语句并将计算放在else语句中,那么这看起来像是一个有效的修复吗?谢谢! – MatthewSpire