是否可以使用jquery的inArray函数检查数组中是否包含多个项目?
if ($.inArray('foo' && 'bar', array) == -1) {
// Neither foo or bar in array
});
由于
答案 0 :(得分:6)
使用jQuery.inArray
,您可以(引用):
在。中搜索指定的值 数组并返回其索引(如果是,则返回-1) 没找到)。
查看该文档页面,似乎您不能将多个值传递给该函数。
那么,为什么不调用该函数两次:'foo'
一次,'bar'
一次:
if ($.inArray('foo', array) == -1 && $.inArray('bar', array) == -1) {
// Neither foo or bar in array
}
答案 1 :(得分:1)
var arr= ['foo','bar'];
var length = arr.length;
for ( var i = 0 ; i < length; i++ ) {
if(jQuery.inArray(arr[i],array) > -1) {
// do whatever you want.
}
}
答案 2 :(得分:0)
怎么样?
if (array.join(",").match(/foo|bar/gi).length == 2){
//
}
或
var find = ["foo","bar"], exp = new RegExp(find.join("|"), "gi");
if (array.join(",").match(exp).length == find.length){
//
}