如何使用jquery隐藏多个值

时间:2021-08-14 20:41:48

I've looked for it but couldn't find the answer i have this :

我找了它但找​​不到答案我有这个:

$("#id option[value='1']").hide();

It'll hide my option number 1 in dropdown but i'm trying to hide more and i could just copy paste this with value 2 etc but i guess i can do this in once ! But how ?

它会在下拉列表中隐藏我的选项编号1,但我试图隐藏更多,我可以复制粘贴这个值2等,但我想我可以一次做到这一点!但是怎么样?

I have tried something like this

我尝试过这样的事情

$("#id option[value='1,2,3']").hide();

but it's not working, also tried

但它没有用,也试过了

    var something = [1,2,3];
    $("#id option[value='something']").hide();

i'm doing something wrong but can't figure out what.

我做错了什么但无法弄清楚是什么。

3 个解决方案

#1


2  

try this following code:

尝试以下代码:

If you want to hide:

如果你想隐藏:

var something = [1,2,3];
$('#id option').filter(function () {
    return ($.inArray(parseInt(this.value), something ) > -1);
}).hide();

and again required show it:

并再次要求显示它:

var something = [1,2,3];
    $('#id option').filter(function () {
        return ($.inArray(parseInt(this.value), something ) > -1);
    }).show();

#2


0  

I think a better solution would be setting a data attribute to the option, categorizing them

我认为更好的解决方案是为选项设置数据属性,对其进行分类

and then hide/show with $('option[data-category="'+X+'"], $('#id')).hide()

然后隐藏/显示$('option [data-category =“'+ X +'”],$('#id'))。hide()

The array map solution (stated in comments) would work too

阵列图解决方案(在评论中说明)也可以

#3


0  

Using JavaScript array:

使用JavaScript数组:

var something = [1,2,3];
var i;
for (i = 0; i < something.length; i++) {
    $("#id option[value='"+something[i]+"']").hide();
}

#1


2  

try this following code:

尝试以下代码:

If you want to hide:

如果你想隐藏:

var something = [1,2,3];
$('#id option').filter(function () {
    return ($.inArray(parseInt(this.value), something ) > -1);
}).hide();

and again required show it:

并再次要求显示它:

var something = [1,2,3];
    $('#id option').filter(function () {
        return ($.inArray(parseInt(this.value), something ) > -1);
    }).show();

#2


0  

I think a better solution would be setting a data attribute to the option, categorizing them

我认为更好的解决方案是为选项设置数据属性,对其进行分类

and then hide/show with $('option[data-category="'+X+'"], $('#id')).hide()

然后隐藏/显示$('option [data-category =“'+ X +'”],$('#id'))。hide()

The array map solution (stated in comments) would work too

阵列图解决方案(在评论中说明)也可以

#3


0  

Using JavaScript array:

使用JavaScript数组:

var something = [1,2,3];
var i;
for (i = 0; i < something.length; i++) {
    $("#id option[value='"+something[i]+"']").hide();
}