I have a simple html block like:
我有一个简单的html块:
<span id="replies">8</span>
Using jquery I'm trying to add a 1 to the value (8).
使用jquery,我尝试向值(8)添加1。
var currentValue = $("#replies").text();
var newValue = currentValue + 1;
$("replies").text(newValue);
What's happening is it is appearing like:
现在的情况是:
81
81年
then
然后
811
811年
not 9, which would be the correct answer. What am I doing wrong?
不是9,这是正确答案。我做错了什么?
10 个解决方案
#1
180
parseInt() will force it to be type integer, or will be NaN (not a number) if it cannot perform the conversion.
parseInt()将强制它为类型整数,如果不能执行转换,则将是NaN(不是一个数字)。
var currentValue = parseInt($("#replies").text(),10);
The second paramter (radix) makes sure it is parsed as a decimal number.
第二个参数(基数)确保它被解析为十进制数。
#2
27
Parse int is the tool you should use here, but like any tool it should be used correctly. When using parseInt you should always use the radix parameter to ensure the correct base is used
解析int是您在这里应该使用的工具,但是与任何工具一样,它应该被正确地使用。使用parseInt时,应该始终使用基数参数以确保使用了正确的基数
var currentValue = parseInt($("#replies").text(),10);
#3
23
The integer is being converted into a string rather than vice-versa. You want:
整数被转换为字符串,而不是相反。你想要的:
var newValue = parseInt(currentValue) + 1
#4
8
parseInt didn't work for me in IE. So I simply used + on the variable you want as an integer.
parseInt在IE中没有为我工作。我只是用+作为整数。
var currentValue = $("#replies").text();
var newValue = +currentValue + 1;
$("replies").text(newValue);
#5
4
In regards to the octal misinterpretation of .js - I just used this...
关于。js的八进制误读,我用了这个…
parseInt(parseFloat(nv))
and after testing with leading zeros, came back everytime with the correct representation.
在用前导0测试之后,每次都返回正确的表示。
hope this helps.
希望这个有帮助。
#6
2
Your code should like this:
您的代码应该如下所示:
<span id="replies">8</span>
var currentValue = $("#replies").text();
var newValue = parseInt(parseFloat(currentValue)) + 1;
$("replies").text(newValue);
黑客N技巧
#7
1
to increment by one you can do something like
要增加1,你可以这样做
var newValue = currentValue ++;
#8
1
var month = new Date().getMonth();
var newmon = month + 1;
$('#month').html((newmon < 10 ? '0' : '') + newmon );
I simply fixed your month issue, getMonth array start from 0 to 11.
我只是修复了你的月问题,getMonth数组从0到11。
#9
1
You can multiply the variable by 1 to force JavaScript to convert the variable to a number for you and then add it to your other value. This works because multiplication isn't overloaded as addition is. Some may say that this is less clear than parseInt, but it is a way to do it and it hasn't been mentioned yet.
可以将变量乘以1,以迫使JavaScript将变量转换为数字,然后将其添加到其他值。这样做是因为乘法没有像加法那样重载。有些人可能会说这比parseInt还不清楚,但这是一种方法,而且还没有被提及。
#10
0
Simply, add a plus sign before the text value
简单地说,在文本值之前添加一个加号
var newValue = +currentValue + 1;
#1
180
parseInt() will force it to be type integer, or will be NaN (not a number) if it cannot perform the conversion.
parseInt()将强制它为类型整数,如果不能执行转换,则将是NaN(不是一个数字)。
var currentValue = parseInt($("#replies").text(),10);
The second paramter (radix) makes sure it is parsed as a decimal number.
第二个参数(基数)确保它被解析为十进制数。
#2
27
Parse int is the tool you should use here, but like any tool it should be used correctly. When using parseInt you should always use the radix parameter to ensure the correct base is used
解析int是您在这里应该使用的工具,但是与任何工具一样,它应该被正确地使用。使用parseInt时,应该始终使用基数参数以确保使用了正确的基数
var currentValue = parseInt($("#replies").text(),10);
#3
23
The integer is being converted into a string rather than vice-versa. You want:
整数被转换为字符串,而不是相反。你想要的:
var newValue = parseInt(currentValue) + 1
#4
8
parseInt didn't work for me in IE. So I simply used + on the variable you want as an integer.
parseInt在IE中没有为我工作。我只是用+作为整数。
var currentValue = $("#replies").text();
var newValue = +currentValue + 1;
$("replies").text(newValue);
#5
4
In regards to the octal misinterpretation of .js - I just used this...
关于。js的八进制误读,我用了这个…
parseInt(parseFloat(nv))
and after testing with leading zeros, came back everytime with the correct representation.
在用前导0测试之后,每次都返回正确的表示。
hope this helps.
希望这个有帮助。
#6
2
Your code should like this:
您的代码应该如下所示:
<span id="replies">8</span>
var currentValue = $("#replies").text();
var newValue = parseInt(parseFloat(currentValue)) + 1;
$("replies").text(newValue);
黑客N技巧
#7
1
to increment by one you can do something like
要增加1,你可以这样做
var newValue = currentValue ++;
#8
1
var month = new Date().getMonth();
var newmon = month + 1;
$('#month').html((newmon < 10 ? '0' : '') + newmon );
I simply fixed your month issue, getMonth array start from 0 to 11.
我只是修复了你的月问题,getMonth数组从0到11。
#9
1
You can multiply the variable by 1 to force JavaScript to convert the variable to a number for you and then add it to your other value. This works because multiplication isn't overloaded as addition is. Some may say that this is less clear than parseInt, but it is a way to do it and it hasn't been mentioned yet.
可以将变量乘以1,以迫使JavaScript将变量转换为数字,然后将其添加到其他值。这样做是因为乘法没有像加法那样重载。有些人可能会说这比parseInt还不清楚,但这是一种方法,而且还没有被提及。
#10
0
Simply, add a plus sign before the text value
简单地说,在文本值之前添加一个加号
var newValue = +currentValue + 1;