如何检查是否字符串是.inarray [英] How to check if string is .inarray

查看:139
本文介绍了如何检查是否字符串是.inarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查是否字符串包含我的阵列?下面是我如何把数组一起...

  //获取选择
 变量$ DD = $('#产品变体'); 如果($ dd.length大于0){//确保我们发现我们要找的选择 //保存选择的值
 变种selectedVal = $ dd.val(); //打通他们的选项和循环
 变量$选项= $('选项',$ DD);
 变种arrVals = [];
 $ options.each(函数(){
     //每个选项的值和文本推到一个数组
     arrVals.push({
         VAL:$(本).VAL()
         文本:$(本)的.text()
     });
 });
};

我要检查,如果凯利绿色包含在阵列内,如果它是我想要.show锂

  $(#选择按颜色列表里#凯利绿色)显示();


解决方案

您可以使用jQuery的内置 .inArray() 方式:

 如果($。inArray(凯利绿色,arrVals)!= -1){
    $(#选择按颜色列表里#凯利绿色)显示()。
}

How can I check if a string is contained with my array? Here's how I'm putting the array together...

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

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

I want to check if "Kelly Green" is contained within the array if it is I want to .show a li

$("#select-by-color-list li#kelly-green").show();

解决方案

You can use jQuery's inbuilt .inArray() method:

if($.inArray('Kelly Green', arrVals) != -1) {
    $("#select-by-color-list li#kelly-green").show();
}

这篇关于如何检查是否字符串是.inarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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