javascript JQuery .inArray() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21355607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:42:57  来源:igfitidea点击:

JQuery .inArray() not working

javascriptjqueryarrays

提问by user2962388

Okay, so I am working on a weather application, and I wrote the following test code to verify that the user entered a valid state abbreviation:

好的,所以我正在开发一个天气应用程序,我编写了以下测试代码来验证用户是否输入了有效的状态缩写:

var input = prompt("What state are you in?");
var lower = input.toLowerCase();
var categories = [ 
    "ma", 
    "ny", 
    "ct", 
    "ri", 
],
var found = $.inArray(lower, categories);
if (found > -1) {
    alert("Cool!");
    }
else {
    alert("Oh no!");
    }

But for some reason, it doesn't work.

但由于某种原因,它不起作用。

Take a look: http://jsfiddle.net/B24Bg/

看一看:http: //jsfiddle.net/B24Bg/

Does anyone know why this is? I probably just made a silly mistake, but any help would be greatly appreciated.

有人知道为什么是这样吗?我可能只是犯了一个愚蠢的错误,但任何帮助将不胜感激。

回答by jammykam

Check your console window in your browser, press F12, there are errors.

在浏览器中检查控制台窗口,按 F12,有错误。

The comma after ] should be a semi-colon:

] 后面的逗号应该是分号:

var input = prompt("What state are you in?");
var lower = input.toLowerCase();
var categories = [ 
    "ma", 
    "ny", 
    "ct", 
    "ri"
];
var found = $.inArray(lower, categories);
if (found > -1) {
    alert("Cool!");
    }
else {
    alert("Oh no!");
    }

jsFiddle Demo

jsFiddle 演示