我似乎无法获取代码来显示我的计算,我是否缺少输出标签?

时间:2019-11-05 04:48:51

标签: javascript html css

我正在尝试用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>

2 个答案:

答案 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)

尝试将您的脚本标签添加到body标签的底部。

enter image description here