1
var bucketId = $.cookie('bucketId');
console.log(bucketId);

var bucketIdNumber = parseInt(bucketId, 10);
console.log("bucketIdNumber " + bucketIdNumber);

在有关 bucketId 的代码中返回“17”。那么为什么当我 parseInt bucketId 得到 NaN 时?

4

2 回答 2

4

If I'm understanding this correctly, the code above is equivalent to:

var bucketId = '"17"';
console.log(bucketId);

var bucketIdNumber = parseInt(bucketId, 10);
console.log("bucketIdNumber " + bucketIdNumber);

In which case, bucketIdNumber does return NaN. You will need to further parse this cookie, and remove any inner quotation marks.

于 2013-01-11T17:32:28.347 回答
1

bucketId probably doesn't start with an integer.

If the first character cannot be converted to a number, parseInt() returns NaN.

于 2013-01-11T17:30:43.760 回答