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

我正在尝试用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>
wwtt_007 回答:我似乎无法获取代码来显示我的计算,我是否缺少输出标签?

嗨,请使用此代码,希望对您有所帮助

谢谢

<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>
,

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

enter image description here

本文链接:https://www.f2er.com/3162218.html

大家都在问