获取文本框的值然后解析它

时间:2012-03-08 05:33:16

标签: javascript html calculator

我在javascript中创建一个简单的计算器,但是为什么每次添加数字时它只会添加第二个数字的前半部分?例如

第一次输入:55 第二个输入:55 然后它会给我一个60的答案。

它只会添加第二个输入的下半部分。

这是我的代码

var fnum;
    var secondNum;
    var operation;

    function msg(val){
        document.getElementById("fnumtext").value += val;
        fnum = val;
    }
    function showOperation(oper){

        document.getElementById("fnumtext").value ="";
        document.getElementById("operation").value = oper;
        operation = oper;
    }

    function equal(){
        secondNum   = document.getElementById("fnumtext").value;
        alert(secondNum+operation);
        if(document.getElementById("operation").value   ==  "+"){
            var x = parseInt(fnum)+parseInt(secondNum);
            alert(x);
        }else if(document.getElementById("operation").value ==  "-"){
            var x = parseInt(fnum)-parseInt(secondNum);
            alert(x);
        }else if(document.getElementById("operation").value ==  "*"){
            var x = parseInt(fnum)*parseInt(secondNum);
            alert(x);
        }else if(document.getElementById("operation").value ==  "/"){
            var x = parseInt(fnum)/parseInt(secondNum);
            alert(x);
        }else{
            alert("choose some Operation");
        }
    }

我的HTML

body>
    <input id = "fnumtext"type="text" name="firstnum" /><br />
    <input id = "operation" type="text" name="secondnum" /><br />
    <input type="button" value="1" onclick="msg('1')" />
    <input type="button" value="2" onclick="msg('2')" />
    <input type="button" value="3" onclick="msg('3')" /></br>
    <input type="button" value="4" onclick="msg('4')" />
    <input type="button" value="5" onclick="msg('5')" />
    <input type="button" value="6" onclick="msg('6')" /><br/>
    <input type="button" value="7" onclick="msg('7')" />
    <input type="button" value="8" onclick="msg('8')" />
    <input type="button" value="9" onclick="msg('9')" /></br>
    <input type="button" value="0" onclick="msg('0')" /></br>
    <input type="button" value="+" onclick="showOperation('+')" />
    <input type="button" value="*" onclick="showOperation('*')" />
    <input type="button" value="/" onclick="showOperation('/')" />
    <input type="button" value="-" onclick="showOperation('-')" />
    <input type="button" value="DO ZEH OPERATION!" onclick="equal()" />
  </body>

2 个答案:

答案 0 :(得分:0)

msg中,您设置fnum的值而不是附加它。尝试将其更改为+=

function msg(val){ 
    document.getElementById("fnumtext").value += val; 
    fnum += val; // changed this line 
}

答案 1 :(得分:0)

首先,您需要跟踪这是您现在进入的第一个操作数还是第二个操作数。 其次,您必须将当前数字的值添加到已累积的值,而不是覆盖它们。

var fnum;
var secondNum;
var operation;
var firstOperand = true;

function msg(val) {
    document.getElementById("fnumtext").value += val;
    if (firstOperand) {
        fnum = document.getElementById("fnumtext").value;
    }
}
function showOperation(oper) 
{
    firstOperand = false;
    document.getElementById("fnumtext").value = "";
    document.getElementById("operation").value = oper;
    operation = oper;
}

function equal() {
    secondNum = document.getElementById("fnumtext").value;
    alert(secondNum+operation);
    if (document.getElementById("operation").value == "+") {
        var x = parseInt(fnum) + parseInt(secondNum);
        alert(x);
    } else if (document.getElementById("operation").value == "-") {
        var x = parseInt(fnum) - parseInt(secondNum);
        alert(x);
    } else if (document.getElementById("operation").value == "*") {
        var x = parseInt(fnum) * parseInt(secondNum);
        alert(x);
    } else if (document.getElementById("operation").value == "/") {
        var x = parseInt(fnum) / parseInt(secondNum);
        alert(x);
    } else {
        alert("choose some Operation");
    }
    firstOperand = true;
    document.getElementById("fnumtext").value = "";
}