我无法将var mpg总数变成“Miles Per Gallon”文本字段。它与var $一起工作,但我试图摆脱快捷方式。我在Stackoverflow或Google上找不到答案。有人能够看到我出错的地方吗?MPG文本字段未使用Javascript进行更新getElementById
<script>
var $ = function (id) {
return document.getElementById(id);
}
var calculateMpg = function() {
var miles = parseFloat($("miles").value);
var gallons = parseFloat($("gallons").value);
if (isNaN(miles) || isNaN(gallons)) {
alert("Both entries must be numeric");
}
else {
var mpg = miles/gallons;
function (id) {
return (document.getElementById("mpg").value).toFixed(3);
}
}
}
window.onload = function() {
$("calculate").onclick = calculateMpg;
$("gallons").focus();
}
</script>
</head>
<body>
<section>
<h1>Calculate Miles Per Gallon</h1>
<label for="miles">Miles Driven:</label>
<input type="text" id="miles"><br>
<label for="gallons">Gallons of Gas Used:</label>
<input type="text" id="gallons"><br>
<label for="mpg">Miles Per Gallon</label>
<input type="text" id="mpg" disabled><br>
<label> </label>
<input type="button" id="calculate" value="Calculate MPG"><br>
</section>