JQuery :not() 选择器结合属性包含选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7080299/
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 23:22:40  来源:igfitidea点击:

JQuery :not() selector combined with Attribute Contains selector

jqueryjquery-selectors

提问by rlemon

So I have a large form and I need to select all elements that have a specific identifier in their id value.

所以我有一个很大的表单,我需要选择所有在其 id 值中具有特定标识符的元素。

$("[id*=some-value]")

This works wonderfully! Now I need to filter out of these results any elements that have another key identifier in their id values

这非常有效!现在我需要从这些结果中过滤掉在其 id 值中具有另一个键标识符的任何元素

$("[id*=some-value]:not([id*=some-other-value])")

which obviously is not working for me.

这显然对我不起作用。

Currently the only element I am filtering is a checkbox so I can just use

目前我过滤的唯一元素是一个复选框,所以我可以使用

$("[id*=add-contact-form]:not(:checkbox)")

however I would still like to know how to combine the two selector methods.

但是我仍然想知道如何组合这两种选择器方法。

采纳答案by David Hancock

What you already have seems to be working fine for me?

你已经拥有的似乎对我来说很好用?

I suggest taking a look at your code and seeing if there is some underlying issue preventing that jQuery selector from working.

我建议查看您的代码,看看是否存在一些阻止 jQuery 选择器工作的潜在问题。

Take a look

看一看

回答by Mrchief

First of all, if your id contains "some-value" literally, then it'll automatically exclude "some-other-value".

首先,如果您的 id 字面上包含“some-value”,那么它会自动排除“some-other-value”。

For it to be able to pick up the other elements, the idhas to match upto a point: "some-other-value" -> "some-value-other" (see how the first 2 portions match)

为了能够获取其他元素,id必须匹配到一个点:“some-other-value”->“ some-value-other”(查看前两个部分如何匹配)

You can try this:

你可以试试这个:

$("[id*=add-contact-form]").not(":checkbox");

or

或者

$("[id*=some-value]").not("[id*=some-value-other]");

DEMO

演示

回答by A.J.

This works for me. Simple, less code, and attractive.

这对我有用。简单,更少的代码,而且很有吸引力。

$('*[id=some_id] option[value!=]')

$('*[id=some_id] option[value!=]')

It will select all the options which have value attribute set to something and ignore with empty ones.

它将选择所有 value 属性设置为某物的选项,并忽略空的选项。