我正在尝试制作一个简单的计算器,但我的函数calculate2()并未被识别为函数。似乎变量FleetSize没有被正确定义为使用var的变量。或者我认为这可能是范围问题。但我无法弄清楚如何解决它。我试过并加倍检查了一切。请帮忙!这是代码:
<html>
<head>
<title>Test</title>
</head>
<body>
<form name="CNGSavingsCalculator">
<table border="1" cellpadding="10" cellspacing="3">
<tr>
<th colspan="2">
<h1>CNG Savings Calculator</h1>
</th>
</tr>
<tr>
<th>
<h3>INPUT</h3>
</th>
<th>
<h3>OUTPUT</h3>
</th>
</tr>
<tr>
<td>
Fleet Size: <input type="number" value="10" name="FleetSize" id="FleetSize" onchange="calculate2();"/> trucks<br>
Average Miles Per Year: <input type="number" value="50000" name="Miles" id="Miles" onchange="calculate2();"> miles<br>
Average Diesel MPG: <input type="number" value="6" name="MPG" id="MPG" onchange="calculate2();"> MPG<br>
Diesel Cost: $<input type="number" value="4.5" name="DieselCost" id="DieselCost" onchange="calculate2();"><br>
Natural Gas Cost: $<input type="number" value="1.75" name="NaturalGasCost" id="NaturalGasCost" onchange="calculate2();"><br>
Incremental NG Truck Cost: $<input type="number" value="50000" name=IncrementalCost" id="IncrementalCost" onchange="calculate2();"><br>
Government Incentives: $<input type="number" value="32000" name="GovernmentIncentives" id="GovernmentIncentives" onchange="calculate2();"><br>
</td>
<td>
<table border="1" cellpadding="10" cellspacing="3">
<tr>
<th>Natural Gas</th>
<th>Diesel</th>
</tr>
<tr>
<td><input type="number" value="99224" name="GallonsNaturalGasUsed" id="GallonsNaturalGasUsed"><br>Gallons Equivalent Used</td>
<td><input type="number" value="83333" name="GallonsDieselUsed" id="GallonsDieselUsed"><br>Gallons Diesel Used</td>
</tr>
<tr>
<td>$<input type="number" value="183102" name="TotalAnnualNaturalGasCost" id="TotalAnnualNaturalGasCost"><br>Total Annual Fuel Cost</td>
<td>$<input type="number" value="375000" name="TotalAnnualDieselCost" id="TotalAnnualDieselCost"><br>Total Annual Fuel Cost</td>
</tr>
</table><br>
<table border="1" cellpadding="10" cellspacing="3">
<tr>
<th>Results</th>
</tr>
<tr>
<td>Payback on Incremental<br>Truck Cost: <input type="number" value="0.94" name="ROI" id="ROI"> Years</td>
</tr>
<tr>
<td>Annual Fuel Savings<br>$<input type="number" value="191898" name="AnnualFuelSavings" id="AnnualFuelSavings"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
function calculate2() {
var FleetSize = document.CNGSavingsCalculator.FleetSize.value,
var Miles = document.CNGSavingsCalculator.Miles.value,
var MPG = document.CNGSavingsCalculator.MPG.value,
var DieselCost = document.CNGSavingsCalculator.DieselCost.value,
var NaturalGasCost = document.CNGSavingsCalculator.NaturalGasCost.value,
var IncrementalCost = document.CNGSavingsCalculator.IncrementalCost.value,
var GovernmentIncentives = document.CNGSavingsCalculator.GovernmentIncentives.value,
GallonsNaturalGasUsed = 10*FleetSize;
GallonsDieselUsed = 20*Miles;
document.CNGSavingsCalculator.GallonsNaturalGasUsed.value = GallonsNaturalGasUsed;
document.CNGSavingsCalculator.GallonsDieselUsed.value = GallonsDieselUsed
}
</script>
</html>
非常感谢!
答案 0 :(得分:2)
您可以在语句开头使用var
来声明其中的所有变量。
您可以使用逗号在var
之后列出多个变量。
您将var
放在,
之后(很多次),这是一个错误。
要么摆脱var
(除了第一个)或用半冒号取代,
来终止声明。
JSHint是一个有用的调试工具。它报道:
3意外'var'。3预期标识符,而不是'var'(a 保留字)。
3缺少分号。
答案 1 :(得分:0)
您使用的语法错误。我的代码片段显示了如何声明变量:
//Either this way:
var a = "something",
b = 1,
c = "another";
//Or this way:
var a = "something";
var b = 1;
var c = "antoher";
提及“,”和“;”你一直在做错。据我所知,你正在反过来使用它; P
您的代码已清理完毕:
function calculate2() {
var FleetSize = document.CNGSavingsCalculator.FleetSize.value,
Miles = document.CNGSavingsCalculator.Miles.value,
MPG = document.CNGSavingsCalculator.MPG.value,
DieselCost = document.CNGSavingsCalculator.DieselCost.value,
NaturalGasCost = document.CNGSavingsCalculator.NaturalGasCost.value,
IncrementalCost = document.CNGSavingsCalculator.IncrementalCost.value,
GovernmentIncentives = document.CNGSavingsCalculator.GovernmentIncentives.value,
GallonsNaturalGasUsed = 10*FleetSize,
GallonsDieselUsed = 20*Miles;
document.CNGSavingsCalculator.GallonsNaturalGasUsed.value = GallonsNaturalGasUsed;
document.CNGSavingsCalculator.GallonsDieselUsed.value = GallonsDieselUsed;
}