开发者

jQuery / javascript variable in if statement not working

I have a variable as such:

var is_last = $('.paging a:last').attr('rel');

th开发者_如何学JAVAis returns '-400' which is correct.

However, i need to add 200 to this so the answer is '-200'

if i do this:

var is_last = $('.paging a:last').attr('rel')+200;

the variable is now '-400200'

How can i pass the variable as a value?

A.


You need to parse the output of .attr(), it to an integer first using parseInt() so you're dealing with a number (and not a string), like this:

var is_last = parseInt($('.paging a:last').attr('rel'), 10) + 200;


I reckon that @Nick Craver is correct and that parseInt is the more correct answer, but as a quick-and-dirty alternative you can also convince javascript that a variable is a number and not a string by multiplying by 1:

    var x = parseInt("-400", 10) + 200;
    var y = ("-400" * 1) + 200;

    alert(x);
    alert(y);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜