jQuery 是否有“或”属性选择器,例如 input[type=button|text]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5982508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:09:40  来源:igfitidea点击:

Is there an "or" attribute selector, like input[type=button|text]?

jqueryjquery-selectors

提问by pimvdb

I'm trying to select all inputelements on a page, but not the ones that are of type image, buttonor submit. What came to my mind first was selecting all inputelements that are of type text, then all of type checkboxetc.

我正在尝试选择input页面上的所有元素,但不选择类型为imagebutton或 的元素submit。我首先想到的是选择所有input类型的元素text,然后选择所有类型checkbox等。

However, this isn't very elegant. I was wondering if there is any better technique here. What would be useful is a selector like input[type=text|checkbox|radio|password|etc], but this does not seem to be available.

然而,这不是很优雅。我想知道这里是否有更好的技术。有用的是像 的选择器input[type=text|checkbox|radio|password|etc],但这似乎不可用。

I know I can also select all inputs and then filter them using .filter()but is there a more generic selector to select elements having one of a list of attributes?

我知道我也可以选择所有inputs,然后使用它们进行过滤,.filter()但是是否有更通用的选择器来选择具有属性列表之一的元素?

回答by Michael Haren

Since you want to include everything excepta few particular types, try this:

由于您想包含少数特定类型之外的所有内容,请尝试以下操作:

$('input:not([type=image],[type=button],[type=submit])')

回答by drudge

$('input[type=text], input[type=checkbox], input[type=radio]').stuff();

or (taken from comments)

或(取自评论)

$('input:not([type=image],[type=button],[type=submit]')

回答by line-o

In CSS you can have selectors like:

在 CSS 中,您可以使用以下选择器:

input[type=text],input[type=checkbox],input[type=radio],input[type=password]

maybe this works in jQuery as well.

也许这也适用于 jQuery。

回答by Kalpesh Popat

I know its old, but i feel the best elegant solution for this would be to give them a common class and then use that as a selector ?

我知道它很旧,但我觉得最好的优雅解决方案是给它们一个通用类,然后将其用作选择器?

回答by Jeec

The above answers are not entirely true.

以上答案并不完全正确。

When depending on a id (#) these methods seems to fail.

当依赖于 id (#) 时,这些方法似乎失败了。

In my experience we need to add the id for each orstatement.

根据我的经验,我们需要为每个or语句添加 id 。

Like so:

像这样:

$('#id input[type=text], #id input[type=checkbox], ...