第一个元素相匹配时,jQuery的inArray不起作用 [英] jQuery inArray doesn't work when the first element matches
问题描述
我想检查我创建作为一个值是否是数组(任意位置)的全部或没有在数组中。如果该值需要做一件事的阵列,在其他地方的另一个。
I'm trying to check an array that I create as to whether or not a value is in the array (anywhere) at all or not. If the value is anywhere in the array it needs to do one thing, else another.
var Arr = [false, false, false, false, false];
// It works with the following:
// Arr = [true, false, false, false, false]
if(!$.inArray(false, Arr))
{
//False is not in the array at all - so continue with code
}
else
{
//False was found in the array
}
所以这上面的code是工作,如果if语句是真实的,但它显然不是。
So this above code is working as if the if statement is true, however it clearly isn't.
如果我改变数组:真的,假的,假的,假的,假的
if语句是假的话,虽然,因为它应该是。
If I change the array to: true, false, false, false, false
the if statement is then false though, as it should be.
基本上我需要这个code做的是,如果阵列中的每个值真正
。
Basically what I need this code to do is to only be true if every value in the array is true
.
推荐答案
$。inArray
返回项目的索引或 1
如果没有产品找到:
$.inArray
returns the index of the item or -1
if no items were found:
if ($.inArray(false, Arr) > -1) {
// found
} else {
// not found
}
首先检查文档总是有用: http://api.jquery.com/jQuery.inArray/
这篇关于第一个元素相匹配时,jQuery的inArray不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!