2016-01-13 53 views
-1

我正在寫一個溫度轉換器,似乎無法弄清楚我的javascript有什麼問題。我甚至無法讓它提醒「跑步」。我的函數曾被稱爲轉換,但我想也許這是js的關鍵詞,所以我將它改爲tempConvert。代碼應該可以相互轉換溫度。我的JavaScript不工作在我的溫度轉換器

兩個測試我使用的是

32F = 0C 

72F = 22.22222...C 

它工作在提示和警報消息很好,現在我想用輸入框。

<!DOCTYPE html /> 

<html> 
<head> 
    <script type="text/javascript">   
     function tempConvert(){ 
      alert("running."); 
      var f = document.getElementById("fahrenheit").value; //Gets the value for farenheit. 
      var c = document.getElementById("celsius").value; //Gets the value for celsius 
      var tf = isNumber(f); //Test if the value of f is a number. 
      var tc = isNumber(c); //Test if the value of c is a number. 

      if (tf = true){ 
//if f is a number run this 
       c = (f-32)/1.8; //conversion from f to c 
       document.getElementById("celsius").value = c; //sets the value of the celsius input box to c. 
       alert(c); 
      } else if (tc = true){ 
// does the same as previous if statement, switching temperature types. 
       f = (c+32)*1.8; 
       document.getElementById("fahrenheit").value = f; 
      } else { 
       alert("One of your inputs are invalid."); 
// alerts the user if f and c are not a number. 
      } 
     } 

     funcion isNumber(test){ //A custom function(method) used to test if f or c is a number 
      return !isNaN(parseFloat(test)) && isFinite(test); //copied from another article in stackoverflow (http://stackoverflow.com/questions/6449611/how-to-check-whether-a-value-is-a-number-in-javascript-or-jquery) 
     } 
    </script> 
</head> 
<body> 
    <input id="fahrenheit" placeholder="fahrenheit"></input> = 
    <input id="celsius" placeholder="celsius"></input> 
    <input type="button" value="convert" onclick="tempConvert()" /> 
</body> 
</html> 
+0

您是否收到任何錯誤?或者函數甚至沒有調用。 – Thangadurai

+0

函數拼寫錯誤在follwoing行中:funcion isNumber(test); –

+0

抱歉代碼沒有執行。 –

回答

1

Working Fiddle

你在你的代碼(一定要聽您的控制檯,看看爲什麼程序不運行)不少語法錯誤。

funcion isNumber(test){ 
^^^^^^^ 
    .... 
} 

這是你拼錯的function這裏。

而且不要忘記===非常不同!在你的代碼中,你不小心設置了tftc,而不是在你的if statements中比較它們。

例如,在這裏:

if (tf = true){ 
    ^
    ... 
} 

應該是:

if (tf == true){ 
     ^^ 
    ... 
} 
+0

Thanks @Nick Zuber –

+0

@JosephLyle很高興能幫到你! –

0

您的isNumber函數中存在拼寫錯誤的函數。如果你改變功能,我很確定這一切都會工作。爲了將來的參考,你可能想看看你的JavaScript控制檯在瀏覽器中診斷這種事情。 Chrome中的Ctrl + shift + J。

0

功能拼寫錯誤funcion isNumber(test);function isNumber(test); 這裏是代碼

<html> 
<head> 
    <script>   
     function tempConvert(){ 
      alert("running."); 
      var f = document.getElementById("fahrenheit").value; //Gets the value for farenheit. 
      var c = document.getElementById("celsius").value; //Gets the value for celsius 
      var tf = isNumber(f); //Test if the value of f is a number. 
      var tc = isNumber(c); //Test if the value of c is a number. 

      if (tf = true){ 
//if f is a number run this 
       c = (f-32)/1.8; //conversion from f to c 
       document.getElementById("celsius").value = c; //sets the value of the celsius input box to c. 
       alert(c); 
      } else if (tc = true){ 
// does the same as previous if statement, switching temperature types. 
       f = (c+32)*1.8; 
       document.getElementById("fahrenheit").value = f; 
      } else { 
       alert("One of your inputs are invalid."); 
// alerts the user if f and c are not a number. 
      } 
     } 

     function isNumber(test){ //A custom function(method) used to test if f or c is a number 
      return !isNaN(parseFloat(test)) && isFinite(test); //copied from another article in stackoverflow (http://stackoverflow.com/questions/6449611/how-to-check-whether-a-value-is-a-number-in-javascript-or-jquery) 
     } 
    </script> 
</head> 
<body> 
    <input id="fahrenheit" placeholder="fahrenheit"></input> = 
    <input id="celsius" placeholder="celsius"></input> 
    <input type="button" value="convert" onclick="tempConvert()" /> 
</body> 
</html> 
0

雖然只是認爲這可以通過調整隻是一點點和移除button。這裏提出一個有點互動的一個問題已經回答是我做的

HTML

<input id="fahrenheit" placeholder="fahrenheit" onkeyup="tempConverter(event)"> = 
<input id="celsius" placeholder="celsius" onkeyup="tempConverter(event)"> 

JS

function tempConverter(event){ 
// using event object to get the parameters 
var getTarget = event.target.id; 
var getTargetValue = event.target.value; 

    //When no input value is present 
    if(getTargetValue ==""){ 
     document.getElementById("celsius").value = ""; 
     document.getElementById("fahrenheit").value = ""; 
    } 

    // populate the relevant input box 
    if(isNumber(getTargetValue)){ 

     if(getTarget ==="fahrenheit"){ 
      document.getElementById("celsius").value = getCelcius(getTargetValue); 
     } 
     else if(getTarget ==="celsius"){ 
      document.getElementById("fahrenheit").value = getFarenheit(getTargetValue); 
     } 
    } 

} 

// Convert from c->f 
function getFarenheit(c){ 
    var f =0; 
    f = 1.8*c+32; 
    return f; 

} 

//Convert from f->c 
function getCelcius(f){ 
    var c = 0; 
    c = (f-32)/1.8; 
    return c; 
} 

//test if it is number 
function isNumber(test){ 
    return !isNaN(parseFloat(test)) && isFinite(test); 
} 

JSFIDDLE