控制台日志中的函数未定义?

时间:2014-08-30 15:11:37

标签: javascript

我正在尝试创建一个工具来转换温度值来自Celsius,Fahrenheit或Kelvin,但是控制台说函数getTemperature()是未定义的?

function getTemperature() {
    var fahrenheit = document.formBox.tempF.value;
    var celsius = document.formBox.tempC.value;
    var kelvin = document.formBox.tempK.value;
    var select = document.formBox.select.value;

    //User gives initial temperature in fahrenheit, convert to C and K
    if (select === "GivenF") {
        var c = (5/9)*(fahrenheit-32);
        var k = ((((fahrenheit-32)*5)/9) + 273.15);
        document.getElementById("celsius").innerHTML = "This equals " + c " degrees celsius.";
        document.getElementById("kelvin").innerHTML = "This equals " + k " kelvin.";
        document.getElementById("fahrenheit").innerHTML = "";
    }
    //User gives initial temperature in celsius, convert to F and K
    else if (select === "GivenC") {
        var f = ((9/5)*celsius)+32;
        var k = celsius+273.15;
        document.getElementById("celsius").innerHTML = "";
        document.getElementById("kelvin").innerHTML = "This equals " + k " kelvin.";
        document.getElementById("fahrenheit").innerHTML = "This equals " + f " fahrenheit.";
    }
    //Use gives initial temperature in kelvin, convert to F and C
    else if (select === "GivenK") {
        var f = (9/5)*(kelvin-273)+32;
        var c = kelvin-273.15;
        document.getElementById("celsius").innerHTML = "This equals " + c " degrees celsius.";
        document.getElementById("kelvin").innerHTML = "";
        document.getElementById("fahrenheit").innerHTML = "This equals " + f " fahrenheit.";
    };
}

1 个答案:

答案 0 :(得分:3)

有语法错误,如

 ... = "This equals " + c " degrees celsius.";

您在变量后错过+符号。

它可能与你调用函数的方式有关,但语法错误是首先。