jQuery inArray总是返回-1 [英] jQuery inArray is always returning -1

查看:595
本文介绍了jQuery inArray总是返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么为什么当lastProductID明显位于数组中时,lastProductIndex仍然为-1!

I cannot figure out why I keep getting -1 for lastProductIndex when clearly the lastProductID is in the array!

var lastProductID = 6758;
var allProductIDs = [5410, 8362, 6638, 6758, 7795, 5775, 1004, 1008, 1013]
var lastProductIndex = $.inArray(lastProductID, allProductIDs);

推荐答案

昨天我遇到了同样的问题,

I had the same problem yesterday,,

var data=[2,4,6,8];
alert( $.inArray(4,data) ) // output 1 as expected
alert( $.inArray("4",data) ) // output -1 as expected 

通常,当您从输入元素或返回字符串的内容中获取要检查的值时,会发生这种情况.您需要执行parseInt(string,radix)将其转换为数字...

Generally this occurs when you get the value to check from a input element or something that returns a string. You need to do parseInt(string,radix) to convert it to a number...

这篇关于jQuery inArray总是返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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