对 Javascript 变量求和(实际上是字符串)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22924324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:02:21  来源:igfitidea点击:

Sum Javascript variables (strings, actually)

javascriptjquery

提问by Hugo

I have three divthat contain a number.

我有三个div包含一个数字。

<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().

我用 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.

自从返回 102030,当时我预计 60。

回答by floribon

First, since you want only the content of your divs, you'd better use $('.x').text()instead of $('.x').html().

首先,因为只需要你的div的内容,你最好使用$('.x').text()代替$('.x').html()

Now what's happening is that you're additioning strings, ie concatening them: '1' + '2' + '3soleil' === '123soleil'.

现在发生什么事是你是additioning串,即concatening他们:'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)

如果您知道它们是整数,则可以更安全地使用Number.parseInt(val, 10)(该函数的第二个变量,称为基数,是数字的数学基数。它很可能是十进制数 (10),但也可能是十六进制数 (16),布尔数 (2) 等)

回答by Travis J

jsFiddle Demo

jsFiddle Demo

You could group them, use each to iterate, and total their parseInt numbers

您可以将它们分组,使用每个进行迭代,并总计它们的 parseInt 数

var val = 0;
$('.a, .b, .c').each(function(){
 val += parseInt(this.innerHTML,10);
});
alert(val);

回答by MamaWalter

use parseIntbecause your div content is evaluate as string:

使用parseInt因为你的 div 内容被评估为字符串:

var val_1 = parseInt($('.a').html(),10), 
    val_2 = parseInt($('.b').html(),10),
    val_3 = parseInt($('.c').html(),10);

回答by Kylok

Use parseInt()on each variable; right now it's treating them as strings and concatenating them.

parseInt()在每个变量上使用;现在它将它们视为字符串并将它们连接起来。

回答by jmiller

You need to parse the string value returned from .html() as an int using parseInt().

您需要使用 parseInt() 将从 .html() 返回的字符串值解析为 int。

var val_1 = parseInt($('.a').html())

回答by Hyman Tuck

If you don't want to type (var + var), you could do the following.

如果您不想键入 (var + var),则可以执行以下操作。

Firstly, the issue in your code was that your variables were interpreted as strings, we can force the INT data-type like, so.

首先,您代码中的问题是您的变量被解释为字符串,我们可以强制使用 INT 数据类型,所以。

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.

然后我们将变量聚合成一个整齐的数组,然后执行.reduce()函数,它是一个求和函数。

var aggregate = array(val_1, val_2, val_3);
aggregate.reduce(function(a,b){return a + b})