javascript - Summing of numbers in jquery returning NAN - Stack Overflow

I'm trying to sum values from an input array but the sum it keeps returning NANvar sum = 0i

I'm trying to sum values from an input array but the sum it keeps returning NAN

var sum = 0    //i have also tried parse(0,10);
$(qty_name).each(function(){
    var aValue = $(this).val(); //i have tried parseInt($(this).val(), 10)
    sum += aValue; //i have tried sum = parseInt(sum) + parseInt(aValue)
});
alert(sum);

i keep getting NaN. I'm ing from a php background so i've never had to deal with type casting. Please what am i missing?

I'm trying to sum values from an input array but the sum it keeps returning NAN

var sum = 0    //i have also tried parse(0,10);
$(qty_name).each(function(){
    var aValue = $(this).val(); //i have tried parseInt($(this).val(), 10)
    sum += aValue; //i have tried sum = parseInt(sum) + parseInt(aValue)
});
alert(sum);

i keep getting NaN. I'm ing from a php background so i've never had to deal with type casting. Please what am i missing?

Share Improve this question edited Sep 11, 2014 at 12:20 Obi Ik asked Sep 11, 2014 at 12:11 Obi IkObi Ik 1592 silver badges9 bronze badges 6
  • Can you show qty_name – Anton Commented Sep 11, 2014 at 12:13
  • Can you add the rest of the script and html? – TeeDeJee Commented Sep 11, 2014 at 12:13
  • var aValue += $(this).val(): initial value of aValue will be undefined, so incrementing will not do anything useful. – Richard Commented Sep 11, 2014 at 12:14
  • did you initialize aValue? – andrew Commented Sep 11, 2014 at 12:14
  • @Richard and andrew sorry that's a mistake. anton & teedejee the qty_name is a variable containing the particular input element i'm getting. it's passing fine, its just the type casting and the summing i'm having issue with – Obi Ik Commented Sep 11, 2014 at 12:18
 |  Show 1 more ment

5 Answers 5

Reset to default 4

That is because your other qty_name do not have perfect integer value. which results to NAN for such values. You need to parse the values to int(or float) for doing any mathematical calucations:

 var sum = 0;
 $(qty_name).each(function(){
   sum += parseInt($(this).val()) || 0;
 });
 alert(sum);

Do like this:

var sum = 0;
$(qty_name).each(function(){
    sum += +($(this).value()); //+ converts to a number
});
alert(sum);

You were doing wrong with var aValue += $(this).val() where you're defining the variable you should not use += operator. You should have done like this:

var sum = 0    //i have also tried parse(0,10);
$(qty_name).each(function(){
    var aValue = $(this).val(); //removed + sign before = sign
    sum += aValue; //i have tried sum = parseInt(sum) + parseInt(aValue)
});
alert(sum);

As far as i understand from your question, you try to avoid adding NaN, use the jQuery built-in method .isNumeric() for that like (and of course += on undefined doesn't end well):

var sum = 0;
$(qty_name).each(function(){
    var aValue = parseInt($(this).val(), 10);
    if(!$.isNumeric(aValue)) return;
    sum += aValue;
});
alert(sum);

Try using isNaN and parseInt as shown :

var sum = 0;
$(qty_name).each(function(){
  sum += (isNaN($(this).val()) ? 0 : parseInt($(this).val(),10)); 
});
alert(sum)

You need to use parseInt or parseFloat, but here you are using var aValue += $(this).val(); and aValue is not initialised. Directly add value to sum

var sum = 0;
$(qty_name).each(function(){
    sum += parseInt($(this).val(), 10)||0;
});
alert(sum);

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

相关推荐

  • javascript - Summing of numbers in jquery returning NAN - Stack Overflow

    I'm trying to sum values from an input array but the sum it keeps returning NANvar sum = 0i

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信