简体   繁体   English

我正在尝试将华氏度转换为摄氏度,但它不起作用我做错了什么? - 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>

Edit1: Moved the inner.HTML into the Function Edit1:将inner.HTML移动到函数中

So the reset button is the only thing that works.所以重置按钮是唯一有效的东西。 Is it possible to calculate the math this way?可以这样计算数学吗?

Adeno Fixed it by declaring what fah was. Adeno 通过声明 fah 是什么来修复它。 you can also see your errors with f12.您还可以使用 f12 查看错误。 https://stackoverflow.com/users/965051/adeneo 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>

You never get the value from the input type = "number"你永远不会从输入中得到值 type = "number"

Try this尝试这个

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

Couple things: You need to set the innerhtml from within the function because the variable is in the function.几件事:您需要从函数内部设置innerhtml,因为变量在函数中。 Or you could have declared the variable outside the function first like var fah = "" then the function.或者您可以先在函数外声明变量,如 var fah = "" 然后是函数。 But since you declared it in the function only the function can see it.但是由于您在函数中声明了它,因此只有函数才能看到它。 So i moved the innerhtml set into the function.所以我将innerhtml集移动到函数中。

Also, javascript likes to use id's not name = "fah" You can call an element by name but id is easier.此外,javascript 喜欢使用 id's not name = "fah" 您可以按名称调用元素,但 id 更容易。

i rounded it to integer.我把它四舍五入为整数。 you would get 9 decimals your way.你会得到 9 位小数。

Lastly, innerhtml set clears all the html so you would lose the °C the way you had it.最后,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>

You asked a question on how to create a pizza form a while ago and you deleted it soon as it was down voted a few times.不久前您问了一个关于如何创建比萨表格的问题,但在多次投票否决后您立即将其删除。

The point to note here is, it is okay if a few people dislike your question.这里要注意的一点是,如果有人不喜欢你的问题,那也没关系。 You've joined StackExchange not to gain points but to learn a few things.您加入 StackExchange 不是为了获得积分,而是为了学习一些东西。 Your question could be helpful to a lot of others out there in this world.您的问题可能对这个世界上的许多其他人有帮助。 So here it is the answer to your pizza question所以这是你的披萨问题的答案

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

Thanks.谢谢。 I hope this helps you.我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM