【发布时间】:2018-07-03 14:30:31
【问题描述】:
我需要一些帮助来完成用户输入数字并将数字放入方程式并求解的表单的 javascript。我很确定我所有的 HTML 都正确完成了,但是 Javascript 代码让我失望了。
感谢任何帮助解决这个问题。我很难完全理解 Javascript。
function calculateMph() {
var feet = document.getElementById("inputFeet").value; //distance in feet
var time = document.getElementById("inputSeconds").value; //speed in seconds
var mph = (45 * feet) / (22 * time); //the equation here
document.write("<p>Your speed in mph is " + mph + "</p>");
}
<h1>Lab: Chapter Two</h1>
<h3>Fill in the form to determine your speed in miles per hour for a particular race that you ran:</h3>
<p><strong>MPH=(15f/22t) <br></strong>where f=distance run in feet and t is time in seconds.</p>
<form id="form">
Distance in Feet(f):<input id="inputFeet" type="text" name="feet" required><br> Number of Seconds(t):<input id="inputSeconds" type="text" name="seconds" required><br><br>
<input type="button" onclick="calculateMph()" value="Calculate" />
</form>
<p><b>Your speed in MPH:</b><br>
<span id="result"></span>
</p>
【问题讨论】:
-
页面加载后切勿使用 document.write。它会擦除页面。而是更新跨度:
document.getElementById("result").innerHTML=mph.toFixed(2); -
还可以阅读以下内容:stackoverflow.com/questions/1458633/…
-
谢谢!我知道我去哪里了。我现在在学校,这对我来说是全新的。如果我将java代码放在html的脚本标签中,我会只使用document.write吗?
-
您可以使用 document.write ONLY inline,例如页面正在加载时
标签: javascript html forms formulas equations