簡體   English   中英

Javascript-從數字字段讀取輸入,執行計算,輸出結果

[英]Javascript - Read input from number field, perform calculation, output result

我是javascript的入門者,只是在尋求一些幫助以解決這個基本問題。 我想從一個字段中獲取華氏溫度數字,將其轉換為攝氏溫度,然后輸出結果。 單擊按鈕沒有任何反應。

HTML:

  function myFunction() { var t0 = document.getElementById('temp1').value; t0 = (t0-32)*.5556; var t1 = document.getElementById('temp2').value; t1 = (t1-32)*.5556; var t2 = document.getElementById('temp3').value; t2 = (t2-32)*.5556; var t3 = document.getElementById('temp4').value; t3 = (t3-32)*.5556; var t4 = document.getElementById('temp5').value; t4 = (t4-32)*.5556; var text = "Temp 1: " + t0 + "<br>"; text += "Temp 2: " + t1 + "<br>"; text += "Temp 3: " + t2 + "<br>"; text += "Temp 4: " + t3 + "<br>"; text += "Temp 5: " + t4 + "<br>"; document.getElementByID("output").innerHTML = text; } 
 <div align="center"> <form id="form2" action="/action_page.php"> <label>Temp 1: </label><input class="field" type="number" id="temp1" name="temp1" /><br> <label>Temp 2: </label><input class="field" type="number" id="temp2" name="temp2" /><br> <label>Temp 3: </label><input class="field" type="number" id="temp3" name="temp3" /><br> <label>Temp 4: </label><input class="field" type="number" id="temp4" name="temp4" /><br> <label>Temp 5: </label><input class="field" type="number" id="temp5" name="temp5" /><br><br> </form> <p>Click the button to convert from Farenheit to Celsius</p> <button onclick="myFunction()">Convert</button> <p id="output"></p> </div> 

除了使用ID修改錯字,更改為Id ,我真的相信這只是您編寫代碼的方式。 確保將腳本放置在head標簽內。 這是一個jsfiddle示例,其head內部具有負載類型: https ://jsfiddle.net/56eet52k/9/

 document.getElementByID("output").innerHTML = text;
You have a typo so correct above line to like this
 document.getElementById("output").innerHTML = text;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM