【发布时间】:2014-10-02 16:33:37
【问题描述】:
我无法将 var mpg total 输入“每加仑英里数”文本字段。它与 var $ 一起使用,但我试图摆脱捷径。我在 Stackoverflow 或 Google 上找不到答案。有人能看出我哪里出错了吗?
<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>
【问题讨论】:
标签: javascript textbox textfield