gpt4 book ai didi

javascript - js,$.inArray,未返回预期返回

转载 作者:行者123 更新时间:2023-11-28 11:17:21 24 4
gpt4 key购买 nike

我想问一个问题,为什么这不起作用?

http://jsfiddle.net/2FMue/4/

var a = $('input').attr('value').split(',');

if(a instanceof Array) alert(1);

if($.inArray($('#id').text(), a)) alert(2);

if($.inArray('United State', a)) alert(3);

// a is array but no return correct match.

我将一个值拆分为数组,并使用 $.inArray 来检查字符串在数组中的位置。

但它不会返回正确的索引。

我不知道这个例子有什么问题。请指教。

非常感谢。

最佳答案

来自文档:

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

所以:

if( $.inArray($('#id').text(), a) != -1) alert(2);

关于javascript - js,$.inArray,未返回预期返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14844549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com