开发者

add using jquery

开发者 https://www.devze.com 2023-01-05 20:03 出处:网络
I want to do this var x=$(this).attr(\'id\'); var y = x+1; where x is an开发者_如何转开发 integer

I want to do this

var x=$(this).attr('id');
var y = x+1;

where x is an开发者_如何转开发 integer

but the value I get is x1

How do I do get the 16, if x=15?

Thanks Jean


All the answers so far are missing the radix parameter

var x=parseInt($(this).attr('id'), 10);
var y = x+1;


var y = parseInt(x) + 1;

should do the trick.


You need to tell JavaScript it's an integer using parseInt

var y = parseInt(x) + 1;


console.log(Number("23") + 1);   //24

I think you should be using Number() instead of parseInt because:

console.log(Number("23#") + 1);   //NaN
console.log(parseInt("23#") + 1); //24 (I would expect a NaN)
0

精彩评论

暂无评论...
验证码 换一张
取 消