如何检查数组中的多个元素有一个,如果使用JavaScript? [英] How to check multiple elements in array with one if with JavaScript?
问题描述
我有一个数组1或多个项目,在这个例子中,让我们说,他们是 [65,66,67]
如何能(如果它甚至有可能)只能做单如果有条件,以检查匹配。
例如:
VAR keyArray = [65,66,67];
如果(e.key code ==在keyArray任何项目){
//然后做这个
}
尽量让一个jQuery插件,用户可将多个键映射到一个单一的动作。例如。 pressing A,B或C可以提醒你是pressing或者是,B或C;
下面是我的实际样品code不工作:
$ this.keydown(函数(五){
如果(在keySplit e.key code){
如果(typeof运算回调=='功能'){
回电话();
亦即preventDefault();
} }
});
有 $。inArray
jQuery的方法这一点。
描述:搜索指定的
阵列中的值,并返回其
索引(或-1,如果未找到)。
块引用>还是看
in_array
JavaScript函数在phpjs。I have 1 or more items in an array, for this example let's say they are
[65, 66, 67]
how can (if it's even possible) do only a single if conditional to check for a match.For example:
var keyArray = [65, 66, 67]; if(e.keyCode == any item in keyArray){ //Then do this }
Try to make a jQuery plugin that a user can map multiple keys to a single action. E.g. pressing a, b, or c could alert "You're pressing either a, b, or c";
Here is my real sample code that isn't working:
$this.keydown(function(e){ if(e.keyCode in keySplit){ if(typeof callback == 'function'){ callback(); e.preventDefault(); } } });
解决方案There is
$.inArray
method in jQuery for that.Description: Search for a specified value within an array and return its index (or -1 if not found).
Or see
in_array
javascript function in phpjs.这篇关于如何检查数组中的多个元素有一个,如果使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!