Adding two numbers concatenates them instead of ca

2018-12-31 05:33发布

I am adding two numbers, I get a correct value.

Got the value after sum

function myFunction() {
  var y = document.getElementById("txt1").value;
  var z = document.getElementById("txt2").value;
  var x = Number(y) + Number(z);
  document.getElementById("demo").innerHTML = x;
}
<p>
  Click the button to calculate x.
  <button onclick="myFunction()">Try it</button>
</p>
<p>
  Enter first number:
  <input type="text" id="txt1" name="text1" value="1">
  Enter second number:
  <input type="text" id="txt2" name="text2" value="2">
</p>
<p id="demo"></p>

20条回答
浅入江南
2楼-- · 2018-12-31 06:17
  <input type="text" name="num1" id="num1" onkeyup="sum()">
  <input type="text" name="num2" id="num2" onkeyup="sum()">
  <input type="text" name="num2" id="result">

  <script>
     function sum()
     {

        var number1 = document.getElementById('num1').value;
        var number2 = document.getElementById('num2').value;

        if (number1 == '') {
           number1 = 0
           var num3 = parseInt(number1) + parseInt(number2);
           document.getElementById('result').value = num3;
        }
        else if(number2 == '')
        {
           number2 = 0;
           var num3 = parseInt(number1) + parseInt(number2);
           document.getElementById('result').value = num3;
        }
        else
        {
           var num3 = parseInt(number1) + parseInt(number2);
           document.getElementById('result').value = num3;
        }

     }
  </script>
查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 06:18

I just use Number():

var i=2;  
var j=3;  
var k = Number(i) + Number(j); // 5  
查看更多
孤独总比滥情好
4楼-- · 2018-12-31 06:20

You are missing the type conversion during the addition step...
var x = y + z; should be var x = parseInt(y) + parseInt(z);

 <!DOCTYPE html>

 <html>
 <body>
  <p>Click the button to calculate x.</p>
  <button onclick="myFunction()">Try it</button>
  <br/>
  <br/>Enter first number:
  <input type="text" id="txt1" name="text1">Enter second number:
  <input type="text" id="txt2" name="text2">
  <p id="demo"></p>
 <script>
    function myFunction() 
    {
      var y = document.getElementById("txt1").value;
      var z = document.getElementById("txt2").value;
      var x = parseInt(y) + parseInt(z);
      document.getElementById("demo").innerHTML = x;
    }
 </script>
 </body>
 </html>
查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 06:20

if we have two input fields then get the values from input fields then add them using javascript.

$('input[name="yourname"]').keyup(function(event) {
    /* Act on the event */ 
    var value1 = $(this).val();     
    var value2 = $('input[name="secondName"]').val();
    var roundofa = +value2+ +value1;

    $('input[name="total"]').val(addition);             
});
查看更多
后来的你喜欢了谁
6楼-- · 2018-12-31 06:21

Here goes your Code by parsing the variables in the function.

    <html>
      <body>
        <p>Click the button to calculate x.</p>
        <button onclick="myFunction()">Try it</button>
        <br/>
        <br/>Enter first number:
        <input type="text" id="txt1" name="text1">
        <br>Enter second number:
        <input type="text" id="txt2" name="text2">
        <p id="demo"></p>
        <script>
          function myFunction() {
            var y = parseInt(document.getElementById("txt1").value);
            var z = parseInt(document.getElementById("txt2").value);
            var x = y + z;
            document.getElementById("demo").innerHTML = x;
          }
        </script>
      </body>
    </html>

Answer

enter image description here

查看更多
怪性笑人.
7楼-- · 2018-12-31 06:22

Use parseInt(...) but make sure you specify a radix value; otherwise you will run into several bugs (if the string begins with "0", the radix is octal/8 etc.).

var x = parseInt(stringValueX, 10);
var y = parseInt(stringValueY, 10);

alert(x + y);

Hope this helps!

查看更多
登录 后发表回答