【问题标题】:Creating a form in html and Javascript solving a formula and getting the answer [duplicate]在 html 和 Javascript 中创建表单解决公式并获得答案 [重复]
【发布时间】: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


【解决方案1】:

只需更改您的代码

document.write("<p>Your speed in mph is " + mph + "</p>");

有了这个

document.getElementById("result").innerHTML = mph.toFixed(2);

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.getElementById("result").innerHTML = mph.toFixed(2);
}
<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>

【讨论】:

    【解决方案2】:

    您没有通过清除页面指定打印结果和 document.write 结果的位置

    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.getElementById("result").innerHTML= mph ;
    }
    <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>

    【讨论】:

    • 哦,我知道我哪里出错了!我很感激。为了清楚起见,如果我在 html 文档的脚本标签中使用它,我是否只需要 document.write?
    • 感谢您的帮助!
    • 请注意,这与stackoverflow.com/a/48424413/295783 几乎相同,这是一个更好的答案,恰好与我的评论相同
    猜你喜欢
    • 2013-04-12
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多