如何将存储在字符串中的数值相加?

时间:2022-08-27 18:16:39

I'm trying to add 1 to my JavaScript variable, but the following code doesn't give expected results:

我想在我的JavaScript变量中添加1,但是下面的代码并没有给出预期的结果:

var val1 = document.getElementById('<%= rng1.ClientID %>');
var val2 = val1.value + "1";
alert(val2.value);

How can I do this?

我该怎么做呢?

5 个解决方案

#1


18  

you can use the function parseInt() to transform the string stored in val1.value into a number:

您可以使用parseInt()函数来转换存储在val1中的字符串。值到一个号码:

var val2 = parseInt(val1.value) + 1;

Note that Number(val1.value) will also convert your value into a number.

注意,Number(val1.value)也会将你的值转换成一个数字。

-edit-
As yc suggest, is highly recommended to use parseInt(val1.value, 10) + 1; because if your string starts with a "0" the string is interpreted as octal. eg. parseInt("0123") == 83 and not 123

如yc建议,强烈建议使用parseInt(val1)。值,10)+ 1;因为如果你的字符串以“0”开头,那么这个字符串就被解释为八进制。如。parseInt("0123") = 83而不是123

#2


5  

The simplest and also preferred way would be to use + to cast to a number.

最简单也是最喜欢的方法是使用+来转换一个数字。

Examples:

例子:

+'234'; // 234
+'042'; // 42! It does not convert to octal

In your case:

在你的例子:

var val1 = +(document.getElementById('<%= rng1.ClientID %>').value); // () added just for the looks
var val2 = val1.value + 1; // if val1 is 5, val2 will be 6 hooray!
alert(val2); // just val2, since it's a plain number not an html input element

#3


1  

parseInt($('.scpContainer').height()+200)

#4


0  

you must add number, so try it without quotes :-)

你必须添加数字,所以尝试它没有引号:-)

#5


0  

In an external JavaScript file you can't use the <%=ControlID.ClientID %> to get the client id.

在外部JavaScript文件中,不能使用<%=ControlID。ClientID %>获取客户端id。

If there you using an external JavaScript file you need to pass client id value to that function.

如果使用外部JavaScript文件,则需要将客户端id值传递给该函数。

#1


18  

you can use the function parseInt() to transform the string stored in val1.value into a number:

您可以使用parseInt()函数来转换存储在val1中的字符串。值到一个号码:

var val2 = parseInt(val1.value) + 1;

Note that Number(val1.value) will also convert your value into a number.

注意,Number(val1.value)也会将你的值转换成一个数字。

-edit-
As yc suggest, is highly recommended to use parseInt(val1.value, 10) + 1; because if your string starts with a "0" the string is interpreted as octal. eg. parseInt("0123") == 83 and not 123

如yc建议,强烈建议使用parseInt(val1)。值,10)+ 1;因为如果你的字符串以“0”开头,那么这个字符串就被解释为八进制。如。parseInt("0123") = 83而不是123

#2


5  

The simplest and also preferred way would be to use + to cast to a number.

最简单也是最喜欢的方法是使用+来转换一个数字。

Examples:

例子:

+'234'; // 234
+'042'; // 42! It does not convert to octal

In your case:

在你的例子:

var val1 = +(document.getElementById('<%= rng1.ClientID %>').value); // () added just for the looks
var val2 = val1.value + 1; // if val1 is 5, val2 will be 6 hooray!
alert(val2); // just val2, since it's a plain number not an html input element

#3


1  

parseInt($('.scpContainer').height()+200)

#4


0  

you must add number, so try it without quotes :-)

你必须添加数字,所以尝试它没有引号:-)

#5


0  

In an external JavaScript file you can't use the <%=ControlID.ClientID %> to get the client id.

在外部JavaScript文件中,不能使用<%=ControlID。ClientID %>获取客户端id。

If there you using an external JavaScript file you need to pass client id value to that function.

如果使用外部JavaScript文件,则需要将客户端id值传递给该函数。