我試圖找到我的代碼不工作的原因,並且chrome返回了這個錯誤: 未捕獲的ReferenceError:calcMPG未定義... 有人能發現我的錯誤嗎?絕望)javascript錯誤:函數未定義
<script type="text/javascript">
function calcMPG() {
document.calc.startingMileage.value = startMileage;
document.calc.endingMileage.value = endMileage;
document.calc.gallonsUsed.value = gallUsed;
var MPG = (endMileage - startMileage)/gallUsed;
if (isNAN(startMileage) || isNAN(endMileage) || isNAN(gallUsed)) alert('please type in numbers only!');
else document.calc.milesPerGalon.value = MPG;
}
</script>
<form name="calc">Starting mileage:
<input type="text" value="0" name="startingMileage" onchange="calcMPG()">
<br>Ending mileage:
<input type="text" value="0" name="endingMileage" onchange="calcMPG()">
<br>Gallons used:
<input type="text" value="0" name="gallonsUsed" onchange="calcMPG()">
<br>Miles per galon:
<input type="text" value="0" name="milesPerGalon">
</form>
startMileage是不確定的。請填寫完整代碼。 – Dmitry
當我運行這個時,我得到錯誤「未捕獲的ReferenceError:startMileage未定義」,而不是您指定的calcMPG。 –
什麼是'startMileage','endMileage'和'gallUsed'? –