2013-01-20 60 views
-1

修復了我的代碼。但是,現在警報彈出說你的價值是NaN。但是,它是一個數字。 parseInt,有一點幫助。我很可能錯過了一些東西。現在我的代碼如下所示:添加不正確顯示

<input id="a" type="text"> 


    <script> 
     function myFunction() 
      { 
       x=document.getElementById("a").value; 
       if(x==""||isNaN(x)) 
        { 
         alert("Your First Number is Not Numeric"); 
        } 
       a=document.getElementById("a").value; 
      } 
    </script> 

    <button type="button" onclick="myFunction()">Submit Number</button> 
    <br /> 
    <p>Second Number</p> 
    <br /> 
    <input id="b" type="text"> 
    <script> 
     function myFunction1() 
      { 
       x=document.getElementById("b").value; 
       if(x==""||isNaN(x)) 
        { 
         alert("Your Second Number is Not Numeric"); 
        } 
       b=document.getElementById("b").value; 
      } 
    </script> 

    <button type="button" onclick="myFunction1()">Submit Number</button> 
    <br /> 
    <script> 
     function add() 
      { 
       d=parseInt(a); 
       e=parseInt("b"); 
       c=d+e; 
       alert("The number is " + c); 
      } 
    </script> 
    <button type="button" onclick="add()">Add numbers</button> 
+2

java!= javascript。 –

+0

請注意,JavaScript與Java不一樣。 –

回答

2

輸入值字符串。使用parseInt或其他方式將它們轉換爲數字,然後嘗試添加它們。

1

輸入的值是一個字符串,而不是數字。在添加字符串之前,您需要使用parseInt()parseFloat()將字符串轉換爲數字。

+0

謝謝我明白了! – Patrick