我正在尝试将华氏温度转换为摄氏温度,但它不起作用我做错了什么? - Javascript(需要有一个表单和一个清除按钮)

时间:2017-02-02 03:03:13

标签: javascript html temperature

<html>
  <head>
    <title>
    Form
     </title>
  </head>
  <body>
      <script type="text/javascript">
      function calculate (form)
  {
   cel = fah * 0.5555 - 32;
   document.getElementById("finish").innerHTML = cel;
  }

</script>

<form name="myform" action="" method="get"> Turn Fahrenheit to Celsius! <br>
<input type="number" name="fah">
<input type="button" name="button" value="calculate" onClick="calculate(this.form)">
<button type="reset" value="Reset">Reset</button>
</form>
<p id="finish">°C</p>
  </body>
</html>

Edit1:将inner.HTML移动到Function

所以重置按钮是唯一可行的。是否有可能以这种方式计算数学?

4 个答案:

答案 0 :(得分:0)

Adeno通过宣布fah是什么来修正它。您还可以使用f12查看错误。 https://stackoverflow.com/users/965051/adeneo

<html>
 <head>
    <title>
        Form
    </title>
</head>
<body>
    <script type="text/javascript">
        function calculate(form) {
            var cel = (document.getElementById("fah").value -32) * 5 / 9;
            document.getElementById("finish").innerHTML = cel;
        }
    </script>
    <form name="myform" action="" method="get"> Turn Fahrenheit to Celsius!
        <br>
        <input type="number" name="fah" id="fah">
        <input type="button" name="button" value="calculate" onClick="calculate(this.form)">
        <button type="reset" value="Reset">Reset</button>
    </form>
    <p id="finish">°C</p>
</body>

答案 1 :(得分:0)

您永远不会从输入类型=“数字”

中获取值

试试这个

<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>

    function calculate()
  {
  var fah = document.getElementById('fah').value;

   var cel = parseFloat(fah * 0.5555 - 32);
    document.getElementById("finish").innerHTML = cel;
  }
    </script>
</head>

<body>








<form>
Turn Fahrenheit to Celsius! <br>
<input type="text" name="fah" id="fah">
<input type="button" name="button" value="calculate" onClick="calculate()">
<button type="reset" value="Reset">Reset</button>
</form>
<p id="finish">°C</p>
</body>
</html>

答案 2 :(得分:0)

偶然事项:你需要在函数中设置innerhtml,因为变量在函数中。或者你可以先在函数外声明变量,如var fah =“”然后是函数。但是因为你在函数中声明它只有函数可以看到它。所以我将innerhtml集移动到了函数中。

此外,javascript喜欢使用id而不是name =“fah”你可以按名称调用元素,但id更容易。

我把它四舍五入为整数。你会得到9位小数。

最后,innerhtml集清除了所有的html,因此你会以你的方式失去°C。

<html>
  <head>
    <title>
Form
 </title>
 </head>
<body>
    <script type="text/javascript">

    function calculate (form)
{

     var fah = this.fah.value;
   cel = Math.round((fah-32) / 1.8);

     document.getElementById("finish").innerHTML = cel+"°C";
}

</script>

<form name="myform" action="" method="get"> Turn Fahrenheit to Celsius! <br>
<input type="number" name="fah" id = "fah">
<input type="button" name="button" value="calculate"                 onClick="calculate(this.form)">
<button type="reset" value="Reset">Reset</button>
</form>
<p id="finish"></p>
  </body>
</html>

答案 3 :(得分:0)

你问了一个关于如何在不久前创建披萨形式的问题,你很快就将其删除,因为它已被投票几次。

这里需要注意的是,如果有些人不喜欢你的问题,那也没关系。您加入StackExchange不是为了获得积分,而是为了学习一些东西。你的问题对这个世界上很多其他人都有帮助。所以这就是你的披萨问题的答案

<html>
<body>

<p>A pizza is 13 dollars with no toppings.</p>

<form action="form_action.asp">
<input type="checkbox" name="pizza" value="Pepperoni" id="pep">Pepperoni + 5$<br>
<input type="checkbox" name="pizza" value="Cheese" id="che">Cheese + 4$<br>
<br>
<input type="button" onclick="myFunction()" value="Send order">
<input type="button" onclick="cost()" value="Get cost">
<br><br>
 <input type="text" id="order" size="50"> 
 </form>

<script>
function myFunction() {
var pizza = document.forms[0];
var txt = "";
var i;
for (i = 0; i < pizza.length; i++) {
    if (pizza[i].checked) {
        txt = txt + pizza[i].value + " ";
    }
}
document.getElementById("order").value = "You ordered a pizza with: " + txt;
}

function cost() {
var pep = 5;
var che = 4;
var pizza = 13;
var total = 0;
if (document.getElementById("pep").checked === true) {
  total += pep;
}

if (document.getElementById("che").checked === true) {
  total += che;
}
  document.getElementById("order").value = "The cost is : " + total;
}

</script>

</body>
</html>

感谢。我希望这会对你有所帮助。