2012-06-28 45 views
6

我有一个基本上是计算器的表单。你可以输入一个方程,它会评估它。我还有2个记忆字段(名为m1和m2的文本框),您可以在其中键入内容并保存该值,然后在第一个框中键入表达式时,可以在公式中引用m1或m2,将使用您在记忆字段中输入的数字进行评估。如何检查评估名称是否未定义

问题是,如果您尝试在公式中引用m1或m2并且文本框为空,则会出现未定义的错误。

我一直在旋转我的车轮几个小时试图并进行某种检查,如果方程式评估为undefined,只需显示一个弹出框。我需要这个在原始javascript。任何帮助表示赞赏。

function displayResult(thisElement) 
{ 
    thisElement.value = eval(thisElement.value); <!-- this line throws the error if you use m1 and no m1 is defined, m2 and no m2 is defined, etc --> 
    if(!(thisElement.value>0)) 
    { 
     thisElement.value=0; 
    } 
} 

function mem(v) 
{ 
    v.value = eval(v.value); 
    eval(v.name+"="+v.value); 
} 

<input id="calcFormula" name="calculate" size="40" /> 
<input type="submit" value="Calculate" onclick="displayResult(this.form.calculate);" /> 

<input name="m1" id="m1" size="12" onchange="mem(this);" value="0" /> 
<input name="m2" id="m2" size="12" onchange="mem(this);" value="0" /> 
+0

无看起来太深入这个问题,检查以确保m1.value和m2.value在尝试评估之前没有未定义会有问题吗?编辑:好吧,我现在可以看到为什么这不起作用。我承认这比最初出现的要复杂得多。 –

+0

啊!!!太多的评价! – Mageek

+0

你应该真的使用像Knockout.js这样的库。这太乱了。 –

回答

7

我能想到的三种解决方案:

  1. 你可以假设空M1/M2是指0,所以会有 永远未定义的值。这真的可以简化事情。
  2. 您可以使用regexp首先检查方程中是否有m1或m2,如果存在则检查是否未定义。
  3. 但最好的方法是使用try...catch

的try/catch示例:

try { 
    eval('12+3+m1'); 
} catch (e) { 
    alert(e.message); 
} 
+0

感谢您编辑RobB :) –

+0

我不知道js有try/catch函数。像魅力一样工作。 – Catfish

1

的evalfails,因为它需要从表单字段加载数据,但M1为表单字段没有访问密钥,且m1是没有全局变量。 ,所以它失败了。 创建2个全局变量,并让m1和m2表单将其值存储在变化中。因为你的函数evals M1和M2,但他们被摧毁

您的原始脚本时失败函数结束,因为他们不是全球范围

function displayResult(thisElement) { thisElement.value = eval(thisElement.value); <!-- this line throws the error if you use m1 and no m1 is defined, m2 and no m2 is defined, etc --> if(!(thisElement.value>0)) { thisElement.value=0; } } 
m1=0; 
m2=0; 

<input id="calcFormula" name="calculate" size="40" /> <input type="submit" value="Calculate" onclick="displayResult(this.form.calc ulate);" /> 

<input name="m1" id="m1" onchange="m1=this.value" size="12" value="0" /> <input name="m2" id="m2" size="12" onchange="m2=this.value;" value="0" /> 

为什么你原来的代码失败:

Step 1. M1 is stored in the form. 
step 2. Onchange event is fired 
step 3. Function mem(this) is called 
step 4. Entering scope of function mem 
step 5. Inserting this.name into string 
step 6. Inserting this.value into string 
Step 7. evalling string 
step 7.1 found code m1=1 
step 7.1.1 check window object for variable named m1 
step 7.1.1.1 failed. Create local variable for function mem called m1 
step 7.1.1.2 assign value 1 to scope variable m1 
step 7.1.1.3 exit eval 
step 7.1.1.4 exit function mem(this) 
step 7.1.1.5 check for scoped variables 
step 7.1.1.6 scoped variable found m1 
step 7.1.1.7 destroy scoped variable and free up memory 
step 7.1.2.2 passed. retrieve pointer to window object variable m1 
step 7.1.2.3 assign value 1 to window.m1 
step 7.1.2.4 exit eval 
step 7.1.2.5 exit function mem(this) 
step 7.1.2.6 check for scoped variables 
step 7.1.2.7 none found, resume other tasks. 
+0

错误。这根本不是范围问题。这是eval()一个不存在的名称的问题。 – Catfish

+0

这是一个范围问题。在使用函数的情况下,如果没有全局变量,则将评估变量作为范围。 他的代码完美地传递了var名称,但只在他的函数范围 – Tschallacka

+0

那么为什么它在m1和m2中有值时会工作?当你从不输入m1和m2的值时,我只会遇到问题。这是因为他们没有定义。 – Catfish

相关问题