首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery .inArray()总是真的吗?

jQuery .inArray()总是真的吗?
EN

Stack Overflow用户
提问于 2010-12-30 05:39:23
回答 1查看 24.4K关注 0票数 19

我尝试使用inarray,但它总是返回true?有什么想法吗?(所有的li都显示出来了)

代码语言:javascript
复制
$("#select-by-color-list li").hide();

// get the select
var $dd = $('#product-variants-option-0');

if ($dd.length > 0) { // make sure we found the select we were looking for

    // save the selected value
    var selectedVal = $dd.val();

    // get the options and loop through them
    var $options = $('option', $dd);
    var arrVals = [];
    $options.each(function(){
        // push each option value and text into an array
        arrVals.push({
            val: $(this).val(),
            text: $(this).text()
        });
    });




};

//This is where it is returning true...


if($.inArray('Aqua', arrVals)) {
    $("#select-by-color-list li#aqua").show();
    };
    if($.inArray('Army', arrVals)) {
    $("#select-by-color-list li#army").show();
    };
EN

回答 1

Stack Overflow用户

发布于 2017-02-22 20:53:55

ES6出手相救!虽然不是jQuery,但我认为值得为未来的读者回答……

ES6引入了.includes(),它的工作方式是你认为$.inArray应该工作的方式:

代码语言:javascript
复制
const myArray = ["a", "b", "c"];

console.log(myArray.includes("a")) /* true */
console.log(myArray.includes("d")) /* false */

Array.prototype.includes()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4558086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档