2012-06-28 41 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

相關問題