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

I am trying to convert Fahrenheit to Celsius but it's not working what am i doing wrong? - Javascript (needs to have a form and a clear button)

<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>

编辑 1:将 inner.HTML 移入函数

因此,重置按钮是唯一有效的按钮。可以这样算吗?

Adeno 通过声明 fah 是什么来修复它。您还可以使用 f12 查看您的错误。 https://whosebug.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>

您永远不会从输入类型中获取值 = "number"

试试这个

<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>

一些事情:您需要在函数内部设置内部html,因为变量在函数中。或者您可以先在函数外部声明变量,例如 var fah = "" 然后是函数。但是因为你在函数中声明了它,所以只有函数才能看到它。所以我将内部 html 设置移到了函数中。

此外,javascript喜欢使用 id 而不是 name = "fah"您可以通过名称调用元素,但 id 更容易。

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

最后,innerhtml set 清除了所有 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>

你刚才问了一个关于如何创建披萨表格的问题,你在几次被否决后立即删除了它。

这里要注意的一点是,如果有几个人不喜欢你的问题也没关系。您加入 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>

谢谢。希望对你有帮助。