jQuery的.inArray()始终是真的吗? [英] jQuery .inArray() always true?

查看:118
本文介绍了jQuery的.inArray()始终是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用inarray,但它总是返回true?有任何想法吗? (李所有的都出现了)

  $(#选择按颜色列表里)隐藏()。//获取选择
变量$ DD = $('#产品变型选项-0');如果($ dd.length大于0){//确保我们发现我们要找的选择    //保存选择的值
    变种selectedVal = $ dd.val();    //打通他们的选项和循环
    变量$选项= $('选项',$ DD);
    变种arrVals = [];
    $ options.each(函数(){
        //每个选项的值和文本推到一个数组
        arrVals.push({
            VAL:$(本).VAL()
            文本:$(本)的.text()
        });
    });
};//这是它返回true ...
如果($。inArray(水族,arrVals)){
    $(#选择按颜色列表里#AQUA)显示()。
    };
    如果($。inArray('军',arrVals)){
    $(#选择按颜色列表里#军)显示()。
    };


解决方案

您需要做的:

 如果($ .inArray(水族,arrVals)-1)〜{

或本

 如果($ .inArray(水族,arrVals)!== -1){

返回的 $。inArray()方法 0 基于项目的索引。如果没有项目,则返回 1 ,其中如果()语句将审议真正

从文档:


  

由于JavaScript的对待0作为松散等于假(即0 ==假的,但0!==假),如果我们检查的数组中值presence,我们需要检查,如果它不是等于(或大于)-1



编辑:除了力推这两个值到数组作为一个对象,只需用一个或另一个,所以你必须从中你可以建立一个多重选择器的字符串数组的

一个方法是这样的:

  //创建一个从价值或选择选择文本的Array
VAR arrVals = $ .MAP($ DD [0]可供选项,功能(选择,我){
    返回opt.value || opt.text;
});  //做一个join()方法对阵列构建多重选择器。
$(#+ arrVals.join(',#')).show();

如果数组是这样的:

  ['军','水上','面包'];

选择器产生的样子:

  $(#军,水族#,#面包).show();

I'm trying to use inarray but its always returning true? Any ideas? (all li's are showing)

$("#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();
    };

解决方案

You need to do this:

if( $.inArray('Aqua', arrVals) > -1 ) {

or this:

if( $.inArray('Aqua', arrVals) !== -1 ) {

The $.inArray() method returns the 0 based index of the item. If there's no item, it returns -1, which the if() statement will consider as true.

From the docs:

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.


EDIT: Instead of pushing both values into the array as an object, just use one or the other, so you have an Array of strings from which you can build a multiple selector.

One way is like this:

  // Create an Array from the "value" or "text" of the select options
var arrVals = $.map( $dd[0].options, function( opt, i ){
    return opt.value || opt.text;
});

  // Build a multiple selector by doing a join() on the Array.
$( "#" + arrVals.join(',#') ).show();

If the Array looks like:

['Army','Aqua','Bread'];

The resulting selector will look like:

$( "#Army,#Aqua,#Bread" ).show();

这篇关于jQuery的.inArray()始终是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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