javascript - Why won't my inputs value sum? - Stack Overflow

Im trying to do a sum of values i get from id but it keeps appending second value to first instead of d

Im trying to do a sum of values i get from id but it keeps appending second value to first instead of doing sum as it should.

Example 23+25=2325

Heres my code:

This is the code im using to sum.

        $('input').blur(function() {
            for (var i=1; i<=value; i++) {

              var one = document.getElementById("veb_blocos-"+i).value; 
              var two = document.getElementById("veb_pellet-"+i).value;
              var sum1 = one+two;
              document.getElementById("total1-"+i).value = sum1; 
           };

        });

Im trying to do a sum of values i get from id but it keeps appending second value to first instead of doing sum as it should.

Example 23+25=2325

Heres my code:

This is the code im using to sum.

        $('input').blur(function() {
            for (var i=1; i<=value; i++) {

              var one = document.getElementById("veb_blocos-"+i).value; 
              var two = document.getElementById("veb_pellet-"+i).value;
              var sum1 = one+two;
              document.getElementById("total1-"+i).value = sum1; 
           };

        });
Share Improve this question edited May 24, 2014 at 8:45 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Jul 27, 2012 at 18:01 João CostaJoão Costa 5192 gold badges7 silver badges20 bronze badges 3
  • [TIP] When posting this kind of question, please create a fiddle jsfiddle to help us creating an easier/faster resolution. – axcdnt Commented Jul 27, 2012 at 18:07
  • Addition vs. Concatenation – jbabey Commented Jul 27, 2012 at 18:11
  • Related: why do I get 24 when adding 2 + 4 in javascript – Felix Kling Commented May 24, 2014 at 8:45
Add a ment  | 

5 Answers 5

Reset to default 7

Try this:

var one = parseInt(document.getElementById("veb_blocos-"+i).value, 10); 
var two = parseInt(document.getElementById("veb_pellet-"+i).value, 10);

Because the value of an input is a string. Cast it to int.

$('input').blur(function() {
    for (var i=1; i<=value; i++) {

      var one = document.getElementById("veb_blocos-"+i).value; 
      var two = document.getElementById("veb_pellet-"+i).value;
      var sum1 = parseInt(one,10)+parseInt(two,10);
      document.getElementById("total1-"+i).value = sum1; 
   };

});

Here is the safest possible solution (presuming the requested DOM nodes are present):

$('input').blur(function () {
    var i = 0,
        one = 0,
        two = 0;
    for (i = 1; i <= value; i += 1) {
        one = Number(document.getElementById("veb_blocos-" + i).value);
        two = Number(document.getElementById("veb_pellet-" + i).value);
        if (isNaN(one)) {
            one = 0;
        }
        if (isNaN(two)) {
            two = 0;
        }
        document.getElementById("total1-" + i).value = one + two;
    };
});

Try:

$('input').blur(function() {
            for (var i=1; i<=value; i++) {

              var one = parseInt(document.getElementById("veb_blocos-"+i).value); 
              var two = parseInt(document.getElementById("veb_pellet-"+i).value);
              var sum1 = one+two;
              document.getElementById("total1-"+i).value = sum1; 
           };

        });

It's because your values are string datatypes instead of a number type. You'll need to parse them first.

$('input').blur(function() {
            for (var i=1; i<=value; i++) {

              var one = parseFloat(document.getElementById("veb_blocos-"+i).value); 
              var two = parseFloat(document.getElementById("veb_pellet-"+i).value);
              var sum1 = one+two;
              document.getElementById("total1-"+i).value = sum1; 
           };

        });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745437497a4627676.html

相关推荐

  • javascript - Why won&#39;t my inputs value sum? - Stack Overflow

    Im trying to do a sum of values i get from id but it keeps appending second value to first instead of d

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信