如何将带有 javascript (jquery) 的整数值添加到返回字符串的值中? [英] How do I add an integer value with javascript (jquery) to a value that's returning a string?

查看:31
本文介绍了如何将带有 javascript (jquery) 的整数值添加到返回字符串的值中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 html 块,例如:

I have a simple html block like:

<span id="replies">8</span>

使用 jquery 我试图给值 (8) 添加一个 1.

Using jquery I'm trying to add a 1 to the value (8).

var currentValue = $("#replies").text();
var newValue = currentValue + 1;
$("replies").text(newValue);

发生的事情看起来像:

81

然后

811

不是 9,这将是正确答案.我做错了什么?

not 9, which would be the correct answer. What am I doing wrong?

推荐答案

parseInt() 将强制其为整数类型,如果无法执行转换,则为 NaN(非数字).

parseInt() will force it to be type integer, or will be NaN (not a number) if it cannot perform the conversion.

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

第二个参数(基数)确保它被解析为十进制数.

The second paramter (radix) makes sure it is parsed as a decimal number.

这篇关于如何将带有 javascript (jquery) 的整数值添加到返回字符串的值中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆