Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

javascript - 如何使用javascript(jquery)将整数值添加到返回字符串的值?(How do I add an integer value with javascript (jquery) to a value that's returning a string?)

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.(不是9,这将是正确的答案。)

What am I doing wrong?(我究竟做错了什么?)   ask by rball translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

parseInt() will force it to be type integer, or will be NaN (not a number) if it cannot perform the conversion.(parseInt()将强制它为integer类型,如果无法执行转换,则为NaN(不是数字)。)

var currentValue = parseInt($("#replies").text(),10);

The second paramter (radix) makes sure it is parsed as a decimal number.(第二个参数(基数)确保将其解析为十进制数。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...