我的 JavaScript 在我的温度转换器中不工作

My JavaScript in not working in my temperature converter

我正在写一个温度转换器,似乎无法弄清楚我的 javascript 有什么问题。我什至不能让它提醒 "running"。我的函数以前叫convert,但我想这可能是js的关键字,所以我把它改成了tempConvert。该代码应该在彼此之间转换温度。

我使用的两个测试是

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 Whosebug (
        }
    </script>
</head>
<body>
    <input id="fahrenheit" placeholder="fahrenheit"></input> = 
    <input id="celsius" placeholder="celsius"></input>
    <input type="button" value="convert" onclick="tempConvert()" />
</body>
</html>

您在 isNumber 函数中拼错了函数。如果您将 funcion 更改为 function 我很确定这一切都会起作用。为了将来参考,您可能想在浏览器中查看 javascript 控制台以诊断此类问题。 Chrome.

中的 Control+shift+J

Working Fiddle

你的代码中有很多语法错误(一定要听你的控制台并查看为什么你的程序不是运行)。

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

你在这里拼写错误 function

此外,不要忘记 === 非常 不同!在您的代码中,您不小心设置了 tftc 而不是在 if statements.

中比较它们

例如,这里:

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

应该是:

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

函数拼写错误 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 Whosebug (
        }
    </script>
</head>
<body>
    <input id="fahrenheit" placeholder="fahrenheit"></input> = 
    <input id="celsius" placeholder="celsius"></input>
    <input type="button" value="convert" onclick="tempConvert()" />
</body>
</html>

虽然已经回答了一个问题,但我认为只需稍微调整一下并删除 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