我的温度转换javascript出了什么问题?

时间:2017-06-21 01:13:42

标签: javascript selection

我是javascript的新手,我想制作一个转换温度的简单代码,我似乎无法找到错误,请帮忙吗? 我觉得它是所选择的财产:p 谢谢xx

这是我的代码:

 <!DOCTYPE html>
<html>
<head>
<title>JC 4</title>
<script type="text/javascript">
    // VARIABLEz 
    var num = document.getElementById("number"); 
    var res = document.getElementById("result"); 

    // METHODz 
    // K E L V I N
    function k2C() {
         res.value = num.value -273.15 ;
    }

    function k2F() {
        res.value = (num.value *(9/5)) - 459.67 ;
    }
    // F A R E N H E I T
    function f2C() {
        res.value = (num.value -32)*(5/9); 
    }

    function f2K() {
        res.value = (num.value +459.67)*(5/9);
    }
    // C E L S I U S
    function c2F() {
        res.value = (num.value *(9/5))+32 ;
    }
    function c2K() {
        res.value = num.value + 273.15 ;
    }

    function clicked1() {
        cf.selected = false ; 
        kc.selected = false ; 
        kf.selected = false ; 
        fc.selected = false ; 
        fk.selected = false ; 
    }

    function clicked2() {
        ck.selected = false ; 
        kc.selected = false ; 
        kf.selected = false ; 
        fc.selected = false ; 
        fk.selected = false ;
    }

    function clicked3() {
        ck.selected = false ;
        cf.selected = false ;  
        kf.selected = false ; 
        fc.selected = false ; 
        fk.selected = false ;
    }

    function clicked4() {
        ck.selected = false ;
        cf.selected = false ; 
        kc.selected = false ;  
        fc.selected = false ; 
        fk.selected = false ;
    }

    function clicked5() {
        ck.selected = false ;
        cf.selected = false ; 
        kc.selected = false ; 
        kf.selected = false ;  
        fk.selected = false ;
    }

    function clicked6() {
        ck.selected = false ;
        cf.selected = false ; 
        kc.selected = false ; 
        kf.selected = false ; 
        fc.selected = false ; 
    }

    function valid() {
        if (ck.selected) {
            c2K();
        }else if (cf.selected) {
            c2F();
        }else if (kc.selected) {
            k2C();
        }else if (kf.selected) {
            k2F();
        }else if (fc.selected) {
            f2C();
        }else if (fk.selected) {
            f2K();
        }
    }
</script>


</head>

<body>
<input type="number" id="number">
<input type="number" id="result" disabled>
<button onclick="valid()"> convert </button>
<select id="MySelect">
    <option id="ck" onclick="clicked1()"> Celsius To Kelvin </option>
    <option id="cf" onclick="clicked2()"> Celsius To Fahrenheit </option>
    <option id="kc" onclick="clicked3()"> Kelvin To Celsius </option>
    <option id="kf" onclick="clicked4()"> Kelvin To Fahrenheit </option>
    <option id="fc" onclick="clicked5()"> Farenheit To Celsius </option>
    <option id="fk" onclick="clicked6()"> Farenheit To Kelvin </option>
</select>



</body>
</html>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
    <title>JC 4</title>
</head>

<body>
    init value:
    <input type="number" id="number">
    result value:
    <input type="number" id="result" disabled>
    <button onclick="valid()"> convert </button>
    <select id="MySelect">
        <option id="ck" value="ck"> Celsius To Kelvin </option>
        <option id="cf" value="cf"> Celsius To Fahrenheit </option>
        <option id="kc" value="kc"> Kelvin To Celsius </option>
        <option id="kf" value="kf"> Kelvin To Fahrenheit </option>
        <option id="fc" value="fc"> Farenheit To Celsius </option>
        <option id="fk" value="fk"> Farenheit To Kelvin </option>
    </select>
    <script type="text/javascript">
        // VARIABLEz 
        // METHODz 
        // K E L V I N
        function k2C(kvalue) {
            return kvalue - 273.15;
        }

        function k2F(kvalue) {
            return (kvalue * (9 / 5)) - 459.67;
        }
        // F A R E N H E I T
        function f2C(fvalue) {
            return (fvalue - 32) * (5 / 9);
        }

        function f2K(fvalue) {
            return (fvalue + 459.67) * (5 / 9);
        }
        // C E L S I U S
        function c2F(cvalue) {
            return (cvalue * (9 / 5)) + 32;
        }

        function c2K(cvalue) {
            return cvalue + 273.15;
        }

        function valid() {
            var num = document.getElementById("number");
            var res = document.getElementById("result");
            var convertType = document.getElementById("MySelect").value;

            switch(convertType) {
                case "ck":
                    res.value = c2K(Number(num.value))
                    break;
                case "kc":
                    res.value = k2C(Number(num.value))
                    break;
                case "kf":
                    res.value = k2F(Number(num.value))
                    break;
                case "fc":
                    res.value = f2C(Number(num.value))
                    break;
                case "fk":
                    res.value = k2F(Number(num.value))
                    break;

            }
        }
    </script>
</body>

</html>
&#13;
&#13;
&#13;

希望能帮到你。