jQuery inArray is always returning -1

后端 未结 6 987
遥遥无期
遥遥无期 2020-12-09 09:40

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

var lastProductID = 6758;
var allProductIDs =          


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 10:11

    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 
    

    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...

提交回复
热议问题