eval函数在JavaScript中不能正常工作

eval function does not work correctly in JavaScript

本文关键字:常工作 工作 不能 函数 JavaScript eval      更新时间:2023-09-26

我是JavaScript新手&我正在创建一个简单的计算器。

但是我对eval()函数有一些问题。

我的脚本:

function calc(fld){
    var firstNo = 0 ;
    var secNo   = 0 ;
    var num = fld.name.charAt(2);
    var op  = fld.name.charAt(0);
    if(op == "t"){op = "-";}
    else if(op == "z"){op = "*";}
    else if(op == "e"){op = "=";}
    else if(op == "j"){op = "+";}
    else if(op == "d"){op = "/";}
    else { op ="";}
    if (op != "=") {eval("document.calc1.res").value  += num +  op ;}
    else { 
        // This line doesn't work correctly
        document.calc1.res.value = eval("document.calc1.res.value") ;
        // nor this one
        // document.calc1.res.value = eval("document.calc1.res").value ;    
    }

这是HTML:

<form id="calc" name="calc1" method="post">
        <input type="text" name="res" id="res"><br />
        <input type="button" value="7" id="no7"  name="no7" onclick="calc(this)"></input type="button" value=""><input type="button" value="8" id="no8" name="no8" onclick="calc(this)"></input type="button" value=""><input type="button" value="9" id="no9" name="no9" onclick="calc(this)"></input type="button" value=""><br />
        <input type="button" value="4" id="no4" name="no4" onclick="calc(this)"></input type="button" value=""><input type="button" value="5" id="no5" name="no5" onclick="calc(this)"></input type="button" value=""><input type="button" value="6" id="no6" name="no6" onclick="calc(this)"></input type="button" value=""><br />
        <input type="button" value="1" id="no1" name="no1" onclick="calc(this)"></input type="button" value=""><input type="button" value="2" id="no2" name="no2" onclick="calc(this)"></input type="button" value=""><input type="button" value="3" id="no3" name="no3" onclick="calc(this)"></input type="button" value=""><br />
        <input type="button" value="-" id="no1" name="t" onclick="calc(this)">
        <input type="button" value="*" id="no1" name="z" onclick="calc(this)">
        <input type="button" value="=" id="no1" name="e" onclick="calc(this)">
            <!--This line works correctly-->
        <INPUT TYPE="button" NAME="DoIt"  VALUE="  =  " OnClick="document.calc1.res.value = eval(document.calc1.res.value)">
        <input type="button" value="/" id="no1" name="d" onclick="calc(this)">
        <input type="button" value="+" id="no1" name="j" onclick="calc(this)">

    </form>

在HTML代码中有两个等号。我的问题在那里。

当我想在JS文件中评估表达式时,它不起作用,但它在HTML文件中工作。我提到了带注释的行。

这两行有什么不同?

1。不要使用 eval() !你不需要。

2)。要在文档中获得所有表单,请执行[document].forms。然后按名称输入.calc1以获取特定表单,按名称输入.res以获取特定字段。

function calc(num1Elem, opElem, num2Elem){
    var num1 = parseInt(num1Elem.value, 10);
    if (num2Elem) var num2 = parseInt(num2Elem.value, 10);
    var op = opElem.value;
    var num;
    if(op == "-") num = num1-num2;
    else if(op == "*") num = num1*num2;
    else if(op == "=") num = num1;
    else if(op == "+") num = num1+num2;
    else if(op == "/") num = num1/num2;
    else op ="";
    if (op != "") {
        document.forms.calc1.res.value = num;
        //Do NOT use eval():
        // document.forms.calc1.res.value = eval(num1+op+num2);
        // This won't even work if we use the = sign.
    }
    //The else loop just sets something equal to what it already is, so it's not doing anything, so we can omit it.
}
//Type in one of these lines of code and sees what happens now!
calc(document.forms.calc1.no8, document.forms.calc1.e, document.forms.calc1.no7)
calc(document.forms.calc1.no8, document.forms.calc1.j, document.forms.calc1.no7)
calc(document.forms.calc1.no8, document.forms.calc1.t, document.forms.calc1.no7)
calc(document.forms.calc1.no8, document.forms.calc1.z, document.forms.calc1.no7)
calc(document.forms.calc1.no8, document.forms.calc1.d, document.forms.calc1.no7)