寻找更多价值的指数

indexof searching for more value

我想查看一个框的文本,如果它与存储在 ARRAY 中的值匹配,那么我想用它做点什么。

var ARRAY1 = ["Saab", "Volvo", "BMW"];   
if (select[i].innerHTML.indexOf('ARRAY1') != -1){//code here}

如您所见,我希望 indexOf 在匹配数组值之一时给出 true。

只需将 ARRAY1 切换为 select[i].innerHTML

var ARRAY1 = ["Saab", "Volvo", "BMW"];   
if (ARRAY1.indexOf(select[i].innerHTML) != -1){//code here}

您也可以访问这些 js 教程。

http://www.w3schools.com/jsref/jsref_indexof_array.asp

Prototype.includes(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

我猜你想拥有类似

的东西
var ARRAY1 = ["Saab", "Volvo", "BMW"],
    compareFunction = function(e) { return select[i].innerHTML.indexOf(e) !== -1; };   

if (ARRAY1.filter(compareFunction).length) {//code here}

此处 compareFunction 是一个函数,它针对所选项目的 html 在每个数组项目上调用。在遍历数组中的所有项目后,它将 return 仅匹配,因此如果找到匹配项,长度将为正。您可能也有兴趣使用 some 而不是 filter - 取决于您想要什么。