0

我正在尝试从 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>

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

我究竟做错了什么?

谢谢。

4

2 回答 2

1

You're trying to get the value of #c with this line

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

but it's not coming back with something. There are no tags with the id c. I think you mean for it to be

var c = document.getElementById("cHeat");
于 2013-05-01T05:20:01.400 回答
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);
        }

    }
于 2013-05-01T05:32:17.717 回答