我正在尝试用Javascript创建MPG(英里每加仑)计算器。点击“计算”按钮后,似乎无法显示结果。
<html>
<head> <title> Miles per Gallons Calculator</title> </head>
<body bgcolor= "#FFFFFF">
<p><script language="JavaScript"> <!-function calcMPG() { var Miles =
document.form1.txtMiles.value var Gallons =
document.form1.txtGallons.value;
var MPG MPG = Miles/Gallons; document.form1.txtMPG.value = MPG}
// --> </script>
<strong>Miles per Gallons Calculator</strong></p> <p>by </p>
<form name="form1"> <p>Miles <input type="text" size="21" name="txtMiles"></p>
<p>Gallons <input type="text" size="20" name="txtGallons"></p>
<p><input type="button" name="btnCalc" value="Calculate MPG" onclick="calcMPG()"></p>
<p><input type="reset" name="btnClear" value="Clear"></p> <p>Miles Per Gallon
<input type="text" size="20" name="txtMPG"></p>
</form>
</body>
</html>
答案 0 :(得分:0)
嗨,请使用此代码,希望对您有所帮助
谢谢
<html>
<head> <title> Miles per Gallons Calculator</title> </head>
<body bgcolor= "#FFFFFF">
<p><strong>Miles per Gallons Calculator</strong></p> <p>by </p>
<form name="form1"> <p>Miles <input type="text" size="21" name="txtMiles"></p>
<p>Gallons <input type="text" size="20" name="txtGallons"></p>
<p><input type="button" name="btnCalc" value="Calculate MPG" onclick="calcMPG()"></p>
<p><input type="reset" name="btnClear" value="Clear"></p> <p>Miles Per Gallon
<input type="text" size="20" name="txtMPG"></p>
</form>
</body>
</html>
<script language="JavaScript">
function calcMPG() {
var Miles = document.form1.txtMiles.value
var Gallons = document.form1.txtGallons.value;
var MPG = parseInt(Miles)/parseInt(Gallons);
document.form1.txtMPG.value = MPG
}
</script>
答案 1 :(得分:0)