$.post('service.php?getPhotos', function(data){
var photoIds = [];
$.each(data, function(){
photoIds.push(this.id);
});
console.log(_photoId); //7962669392
console.log(photoIds); //["7980686507", "7962669392", "7962163506"]
console.log($.inArray(_photoId, photoIds)); //-1
});
为什么console.log($.inArray(_photoId, photoIds));
不返回1
?
答案 0 :(得分:3)
似乎_photoId
是一个数字,但photoIds
包含字符串。试试这个:
$.inArray(''+ _photoId, photoIds)
答案 1 :(得分:3)
字符串vs整数我想象。不同的类型意味着inArray不会将它们视为相同。确保使用的是字符串而不是整数,这应该可以。
答案 2 :(得分:1)
这是因为$.inArray
函数也考虑了类型。它不会说整数与字符串相同。 documentation中的第一条评论也对此进行了解释。
您可以使用_photoId.toString()
来获取值的字符串表示形式。
另请注意,ECMAScript(Javascript)有一个名为indexOf的本机函数,它与jQuery函数完全相同:
photoIds.indexOf(_photoId.toString())