2013-01-20 63 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