0

这是我使用的 Html 形式。

<form name="calculate" method="get" action="#" onsubmit="return Validate()">
<label>Enter a number</label>
<input id="numb1" name="numb1" id="numb2" placeholder="Number 1">

<label>Enter another number</label>
<input id="numb2" name="numb2" id="numb2" placeholder="Number 2">

<label>Result</label>
<textarea style="text-align:center; height:85px; max-height:85px;" name="summ" id="summ" disabled="disabled" placeholder="Result" dir="ltr"></textarea>
<br />
<input id="calc" name="calc" type="submit" value="Calculate!">

JS

    function Validate() {
        if (calculate.numb1.value == "" || calculate.numb2.value == "") {
            alert("Check the form again");
            calculate.numb1.focus();
            document.getElementById('summ').value = "";
            return (false);
        }
        Update()
        return (true);

    }
    function Update() {
        var plus = calculate.numb1.value + calculate.numb2.value;
        var minus = calculate.numb1.value - calculate.numb2.value;
        var mult = calculate.numb1.value * calculate.numb2.value;
        var div = calculate.numb1.value / calculate.numb2.value;

        var multi = document.getElementById("summ").value = calculate.numb1.value + "+" + calculate.numb2.value + "=" + plus + "\n" + calculate.numb1.value + "-" + calculate.numb2.value + "=" + minus + "\n" + calculate.numb1.value + "*" + calculate.numb2.value + "=" + mult + "\n" + calculate.numb1.value + "/" + calculate.numb2.value + "=" + div;
    }

所有计算都完全接受 Plus 功能。例如,当您键入 4+4 时,您会得到 44... 我尝试了 parseFloat,但仍然没有任何变化。

4

2 回答 2

0
var numb1 = parseFloat(document.calculate.numb1.value);
var numb2 = parseFloat(document.calculate.numb2.value);

var plus = numb1 + numb2;
var minus = numb1 - numb2;
var mult = numb1 * numb2;
var div = numb1 / numb2;
于 2012-12-07T13:49:22.410 回答
0

首先将它们转换为数字。对每个使用字符串值的计算执行此操作。

var plus = Number(calculate.numb1.value) + Number(calculate.numb2.value);

莱昂

于 2012-12-07T13:51:45.017 回答