Javascript,计算器Eval()错误?

时间:2013-10-21 10:28:01

标签: javascript calculator

以下是我的代码。 。

<div id="calc">

<input id="screen"></div>
<div id="subkeys">
<div  class="btn1">log</div>
<div  class="btn1">ln</div>
<div  class="btn1">(-)</div>
<div  class="btn1">sin</div>
<div  class="btn1">cos</div>
<div  class="btn1">tan</div>
<div  class="btn1">sin<sup>-1</sup></div>
<div  class="btn1">cos<sup>-1</sup></div>
<div  class="btn1">tan<sup>-1</sup></div>
</div>
<div id="mainkeys">
<div name="del" value="" onClick="rundel()" class="btn2 btn-del">DEL</div>
<div name="minus" value="-" onClick="runminus()" class="btn2 btn-minus">-</div>
<div name="multi" value="x" onClick="runmulti()" class="btn2 btn-multi">x</div>
<div name="plus" value="+" onClick="runplus()" class="btn2 btn-plus">+</div>
<div name="1" value="1" onClick="addDigit(1)" class="btn2 num">1</div>
<div name="2" value="2" onClick="addDigit(2)" class="btn2 num">2</div>
<div name="3" value="3" onClick="addDigit(3)" class="btn2 num">3</div>
<div name="div" value="÷" onClick="rundiv()" class="btn2 btn-div">÷</div>
<div name="ans" value="ans" onClick="runans()" class="btn2 btn-ans">ANS</div>
<div name="0" value="0" onClick="addDigit(0)" class="btn2 num">0</div>
<div name="eq" value="=" onClick="runeq()" class="btn2 btn-eq">=</div>
</div>

<script>
function addDigit(digit) {  
document.getElementById('output').value += digit;     
}
function rundel(){document.getElementById('output').value = "";}
function runminus(){document.getElementById('output').value = "";}
function runplus(){document.getElementById('output').value = "";}
function rundiv(){document.getElementById('output').value = "";}
function runmulti(){document.getElementById('output').value = "";}
function runeq(){
var result = eval(document.getElementById('output').value = "")
    document.getElementById('output').value = result;
}
</script>

当我试图添加,减去,除以或多个两个数字时,我得到的结果是UNDEFINED,我想要结果即,当我按+键时结果应该是添加掉那些键,如何修复这个错误。 。

Here is Link

2 个答案:

答案 0 :(得分:0)

<script>
function addDigit(digit) {  
document.getElementById('output').value += digit;     
}
function rundel(){document.getElementById('output').value = "";}
function runminus(){document.getElementById('output').value = "";}
function runplus(){document.getElementById('output').value = "";}
function rundiv(){document.getElementById('output').value = "";}
function runmulti(){document.getElementById('output').value = "";}
function runeq(){
var result = eval(document.getElementById('output').value);
    document.getElementById('output').value = result;
</script>

试试这个......

答案 1 :(得分:0)

eval 邪恶 ...你应该尝试永远不要使用eval ..除非绝对必要...... 你可以将最后一个输入和当前操作存储在一个变量..并进行计算......但是......这比简单地使用eval要难得多......

例如......你可以做这样的事情...... jsfiddle demo example

var lastInput = 0; //storing last input
var curOp; // current operation.. plus,minus,div etc...

function addDigit(digit) {
    document.getElementById('output').value += digit;
}

function runeq() {
    var result = 0;
    switch (curOp) {
    case "minus":
        result = lastInput - getValue();
        break;
        case "plus" :
                    result = lastInput + getValue();
        break;
        case "div" :
                    result = lastInput / getValue();
        break;      
        case "multi" :
                    result = lastInput * getValue();
         break;
    default:
        result = 0;
        break;
    }
    document.getElementById('output').value = result;
    curOp = 0 ;
    lastInput = getValue();
}

function rundel(){
    lastInput = 0;
curOp = 0 ;
clearScreen();}
function runminus() {
    curOp = "minus";
    lastInput = getValue();
    //for testing.. 
    document.getElementById('test').value = lastInput;
    clearScreen();
}

function runplus() {
    curOp = "plus";
    lastInput = getValue();
    //for testing.. 
    document.getElementById('test').value = lastInput;
    clearScreen();
}

function rundiv() {
    curOp = "div";
    lastInput = getValue();
    //for testing.. 
    document.getElementById('test').value = lastInput;
    clearScreen();
}

function runmulti() {
    curOp = "multi";
    lastInput = getValue();
    //for testing.. 
    document.getElementById('test').value = lastInput;
    clearScreen();
}

function getValue() {
    return parseInt(document.getElementById('output').value,10) ;
}

function clearScreen() {
    document.getElementById('output').value = "";
}