2015-11-30 155 views
0

我試圖做一個溫度轉換器,你從下拉菜單中選擇一個類型的溫度,然後在溫度輸入,溫度轉換器將其轉換成其他兩種形式的溫度。華氏選項工作正常,但我無法弄清楚如何使攝氏度和Kelvin的選擇工作。與下拉菜單

<!DOCTYPE html> 
<html> 
    <head> 
     <style> 
      body { 
       background-color: #002b80; 
      } 

      p { 
       color: white; 
      } 

     </style> 
    </head> 

    <body> 

     <center> 
     <form> 
      <select id="temperature"> 
       <option value="f">Fahrenheit</option> 
       <option value="c">Celsius</option> 
       <option value="k">Kelvin</option> 
      </select> 
      <input type="text" id="fconvert" onkeyup="myFunction()"> 
     </form> 
     <br> 
     <br> 
     <input type="text" id="cconvert"> 
     <input type="text" id="kconvert"> 
     <br> 
     <br> 
     <span id="demo"></span></p> 

     <div id="temp"> 

     </div> 
     </center> 

     <script> 
      function myFunction() { 
       if(document.getElementById("temperature").value = "f") { 
        convertF(); 

       } else { 
        convertC(); 

       } 
      }  

      function convertF() { 
       var x = document.getElementById("fconvert").value; 
       var convertF = x * 2; 
       var convertC = x * 3; 
       var convertK = x * 4; 

       document.getElementById("cconvert").value = convertF; 
       document.getElementById("kconvert").value = convertF; 

      } 


      function convertC() { 
       var x = document.getElementById("fconvert").value; 
       var convertF = x * 2; 
       var convertC = x * 3; 
       var convertK = x * 4; 

       document.getElementById("cconvert").value = convertC; 
       document.getElementById("kconvert").value = convertC; 

      } 


      function convertK() { 
       var x = document.getElementById("fconvert").value; 
       var convertF = x * 2; 
       var convertC = x * 3; 
       var convertK = x * 4; 

       document.getElementById("cconvert").value = convertK; 
       document.getElementById("kconvert").value = convertK; 

      } 

     </script> 
    </body> 
</html> 
+0

什麼是不工作的預期?此外,你不執行任何計算將溫度轉換。你是否在使用外部服務來做到這一點? – Steve

回答

0

您應該使用===進行比較(==如果不是類型安全)

if(document.getElementById("temperature").value === "f") {