简体   繁体   中英

I can't seem to get my code to display my calculation, am I missing an output tag?

I am trying to create an MPG (Miles Per Gallon) calculator in Javascript. I can't seem to display my results after I hit the "Calculate" button.

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

Hi Please Use this code I hope it's helpful

Thanks

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

Try add your script tag bottom of the body tag.

在此处输入图像描述

you have some syntax error and i prefer to seperate javascript in a new file or put the script after the body tag

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM