2013-10-31 20 views
0

當我在瀏覽器中打開該函數時,我可以輸入我想要的等式,但在顯示答案後,我無法讓計算器重置。拿到NaN後,我被卡住了。任何幫助將非常感激。由於回答後,Javascript計算器將不會清除

<!DOCTYPE html> 
<HTML> 
<HEAD> 
    <script type = "text/javascript"> 
     function divide() 
     { 
      document.calculator.output.value += "/"; 
     } 
     function add() 
     { 
      document.calculator.output.value += "+"; 
     } 
     function num9() 
     { 
      document.calculator.output.value += "9"; 
     } 
     function num8() 
     { 
      document.calculator.output.value += "8"; 
     } 
     function num7() 
     { 
      document.calculator.output.value += "7"; 

     } 
     function multiply() 
     { 
      document.calculator.output.value += "*"; 
     } 
     function percent() 
     { 
      document.calculator.output.value += "%"; 
     } 
     function num6() 
     { 
      document.calculator.output.value += "6"; 
     } 
     function num5() 
     { 
      document.calculator.output.value += "5"; 
     } 

     function num4() 
     { 
      document.calculator.output.value += "4"; 
     } 


     function subtr() 
     { 
      document.calculator.output.value += "-"; 
     } 

     function num3() 
     { 
      document.calculator.output.value += "3"; 
     } 

     function num2() 
     { 
      document.calculator.output.value += "2"; 
     } 

     function num1() 
     { 
      document.calculator.output.value += "1"; 
     } 

     function numZero() 
     { 
      document.calculator.output.value += "0"; 
     } 

     function buildFormula() 
     { 
      var evalu= alert(eval(document.calculator.output.value)) + document.clear(); 
      document.calculator.output.value= evalu; 
     } 


    </script> 
</HEAD> 
<BODY> 
<form name="calculator"> 

<table border=0> 
<tr> 
    <td colspan=4><input type=text readonly=true name="output" size = 25></td> 
</tr> 
<tr> 
    <td><input type=button value= " 7 " onClick="num7()"></td> 
    <td><input type=button value= " 8 " onClick="num8()"></td> 
    <td><input type=button value= " 9 " onClick="num9()"></td> 
    <td><input type=button value= " /" onClick="divide()"></td> 
    <td><input type=button value= "  + " onClick="add()"></td> 
</tr> 
<tr> 
    <td><input type=button value= " 4 " onClick="num4()"></td> 
    <td><input type=button value= " 5 " onClick="num5()"></td> 
    <td><input type=button value= " 6 " onClick= "num6()"></td> 
    <td><input type=button value= " * " onClick= "multiply()"></td> 
    <td><input type=button value= " % " onClick="percent()"></td> 
</tr> 
    <tr> 
     <td><input type=button value = " 1 " onClick="num1()"></td> 
     <td><input type=button value = " 2 " onClick="num2()"></td> 
     <td><input type=button value = " 3 " onClick="num3()"></td> 
     <td><input type=button value = " - " onClick="subtr()"></td> 
     <td><input type=button value = "  =  " onClick="buildFormula()"></td> 
</tr> 
<tr> 
    <td colspan=5><input type=button value = "    0    " onClick="numZero()"></td> 
</tr> 
</table> 
</form> 
</BODY> 
</HTML> 
+0

你在做什麼「重置」? – Floris

+4

如果你有這樣的重複代碼,內心深處應該會告訴你有什麼不對。 –

+0

把它放在小提琴 –

回答

2

你的問題,因爲你想從document.clear()彙總來自alert數據回 - 這是錯誤的,警惕返回任何內容並document.clear是未定義的方法

你函數來這一變化:

function buildFormula() 
    { 
     var evalu= eval(document.calculator.output.value); 
     document.calculator.output.value= ''; 
     alert(evalu); 
    } 
+0

這工作。我也把我的號碼從「」中拿出來。非常感謝。 – The9411

+1

不要感謝他,謝謝複選標記。 – PlantTheIdea

+0

+1 BC我走錯了方向,大聲笑沒有意識到它,直到我把它放在提琴 –