admin管理员组文章数量:1204590
I have three div
that contain a number.
<div class="a">10</div>
<div class="b">20</div>
<div class="c">30</div>
I get the number inside each one with jQuery .html()
.
var val_1 = $('.a').html(),
val_2 = $('.b').html(),
val_3 = $('.c').html();
How can I sum them?
This do not work as expected:
var total = val_1 + val_2 + val_3;
Since returned 102030, when I expected 60.
I have three div
that contain a number.
<div class="a">10</div>
<div class="b">20</div>
<div class="c">30</div>
I get the number inside each one with jQuery .html()
.
var val_1 = $('.a').html(),
val_2 = $('.b').html(),
val_3 = $('.c').html();
How can I sum them?
This do not work as expected:
var total = val_1 + val_2 + val_3;
Since returned 102030, when I expected 60.
Share Improve this question edited Jul 28, 2015 at 9:12 Programmeur asked Apr 7, 2014 at 22:33 ProgrammeurProgrammeur 1,5314 gold badges22 silver badges33 bronze badges 4 |7 Answers
Reset to default 12First, since you want only the content of your divs, you'd better use $('.x').text()
instead of $('.x').html()
.
Now what's happening is that you're additioning strings, ie concatening them: '1' + '2' + '3soleil' === '123soleil'
.
You want to parse them into numbers, which is done with
Number(val1) + Number(val2) + Number(val3)
If you know they're integers, you can more safely use Number.parseInt(val, 10)
(the second variable of that function, called radix, is the mathematic base of your number. It's most likely a decimal number (10), but could be hexadecimal (16), boolean number (2), etc)
jsFiddle Demo
You could group them, use each to iterate, and total their parseInt numbers
var val = 0;
$('.a, .b, .c').each(function(){
val += parseInt(this.innerHTML,10);
});
alert(val);
use parseInt because your div content is evaluate as string:
var val_1 = parseInt($('.a').html(),10),
val_2 = parseInt($('.b').html(),10),
val_3 = parseInt($('.c').html(),10);
Use parseInt()
on each variable; right now it's treating them as strings and concatenating them.
You need to parse the string value returned from .html() as an int using parseInt().
var val_1 = parseInt($('.a').html())
If you don't want to type (var + var), you could do the following.
Firstly, the issue in your code was that your variables were interpreted as strings, we can force the INT data-type like, so.
val_1 = parseInt($('.a').html()),
val_2 = parseInt($('.b').html()),
val_3 = parseInt($('.c').html());
Then we aggregate the variables into a tidy array, then perform the .reduce()
function, which is a sum function.
var aggregate = array(val_1, val_2, val_3);
aggregate.reduce(function(a,b){return a + b})
Use:
var total = parseInt(variable1+variable2);
This is a simple way and I use it to solve the NaN problem in jQuery.
本文标签: jquerySum Javascript variables (stringsactually)Stack Overflow
版权声明:本文标题:jquery - Sum Javascript variables (strings, actually) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738684699a2106756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var val_1 = parseInt($('.a').html(), 10);
In JavaScript,+
is both addition and string concatenation ("addition"). – gen_Eric Commented Apr 7, 2014 at 22:35