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


<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:将内部.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>

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

阿德诺 通过声明 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>

你永远不会从输入类型="数字"中获取值

试试这个

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

几件事:你需要从函数中设置innerhtml,因为变量在函数中。 或者你可以先在函数外部声明变量,如 var fah = ",然后是函数。 但是由于您在函数中声明了它,因此只有函数可以看到它。 所以我将 innerhtml 集移到了函数中。

此外,javascript 喜欢使用 id 的 not 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>

相关内容

最新更新