如何检查是否值不使用jQuery数组 [英] How to check if a value is NOT in an array using jQuery

查看:148
本文介绍了如何检查是否值不使用jQuery数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到一些麻烦检查如果值为不可以在数组中。我试图让一个文本框,只有某些关键presses。

I'm having some trouble with checking if a value is not in an array. I'm trying to allow only certain key presses in a textbox.

下面是我有:

var keyCodeArr = [ 8,9,13,17,18,37,39,46 ];
$('#txtSearch').keydown(function(event) {
var code = event.keyCode;

if (!$.inArray(code,keyCodeArr)) {
    event.preventDefault();
}
});

它允许在文本框中什么。我看着为$ .inArray(),它显示了一个眼看例如文档,如果一个值的在一个数组,所以我想只是增加了不前它会做的伎俩。

It's allowing anything in the textbox. I looked at the documentation for $.inArray(), which show an example for seeing if a value is in an array, so I thought just adding the NOT in front of it would do the trick.

或者是说我没有正确初始化数组中的第一个地方?

Or is it that I'm not initializing the array correctly in the first place?

我是什么失踪?

推荐答案

使用jQuery:

if (!~$.inArray(code,keyCodeArr)) {
    ...
}

这篇关于如何检查是否值不使用jQuery数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆