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 に答える