Javascript - 更改文本字段的值

时间:2013-05-01 05:05:06

标签: javascript

我正在尝试从3个文本字段中获取数据,将它们相乘并将值放在另一个文本字段中。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type='text/javascript'>
        function run()
        {
            var eq = document.getElementById("Eq");
            var m  = document.getElementById("m1");
            var c  = document.getElementById("c");
            var dT = document.getElementById("dT");
            var ek = document.getElementById("Ek");
            var v  = document.getElementById("v");

            if(eq.value.length == 0)
            {
                var result = parseFloat(m.value) * parseFloat(c.value) * parseFloat(dT.value);
                alert(result);
                eq.value = result.toString();

            }
            if(c.value.length == 0)
            {
                c.value = eq.value / (m.value * dT.value);
            }

        }
        function m1change()
        {
            var m1 = document.getElementById("m1");
            var m2 = document.getElementById("m2");
            m2.value = m1.value;

        }
        function m2change()
        {
            var m1 = document.getElementById("m1");
            var m2 = document.getElementById("m2");
            m1.value = m2.value;
        }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Welcome - ברוכים הבאים</title>
</head>
<body>
    <form action="">
        <h1>Eq = m * c * ΔT</h1>
        <input type="number" max="2147483648" id="Eq" maxlength="10" /> = <input type="number" max="10" class="m" id="m1" maxlength="10" onkeyup="m1change()" /> * <input type="number" max="2147483648" id="cHeat" maxlength="10" /> * <input type="number" max="2147483648" id="dT" maxlength="10" />
        <h1>Ek = (m * v²) / 2</h1>
        <input type="number" max="2147483648" id="Ek" maxlength="10" /> = (<input type="number" max="10" class="m" id="m2" maxlength="10" onkeyup="m2change()" /> * <input type="number" max="2147483648" id="v" maxlength="10" /><font size="6.5">²</font>  ) / 2
        <h1>Eh = m * g * h</h1>

        <input type="reset" /> <input type="button" onclick="run()" value="Calculate" />

        <h6>by X</h6>
    </form>
</body>
</html>

它没有提醒结果,所以我认为它停留在结果声明中。

我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:1)

您尝试使用此行获取#c的值

var c  = document.getElementById("c");

但它没有带回来。没有标识为c的标记。我认为你的意思是

var c = document.getElementById("cHeat");

答案 1 :(得分:0)

尝试查看此更新的run()函数...

您缺少一个文本框,需要获取.value属性。 :)

    function run()
    {
        var eq = document.getElementById("Eq").value;
        var m  = document.getElementById("m1").value;
        var c  = document.getElementById("cHeat").value;
        var dT = document.getElementById("dT").value;
        var ek = document.getElementById("Ek").value;
        var m2 = document.getElementById("m2").value;
        var v  = document.getElementById("v").value;

        console.log("eq = " + eq);
        console.log("m = " + m);
        console.log("c = " + c);
        console.log("dT = " + dT);
        console.log("ek = " + ek);
        console.log("m2 = " + m2);
        console.log("v = " + v);


        if(eq.length == 0)
        {
            console.log('a');

            var result = parseFloat(m) * parseFloat(c) * parseFloat(dT);
            alert(result);

            eq = result.toString();

        }
        if(c.length == 0)
        {
            console.log('b');

            c = eq / (m * dT);
        }

    }