jQuery 选择器问题

发布于 2024-10-15 07:18:17 字数 978 浏览 1 评论 0原文

<input type="radio" id="rad1" name="group" value="one">First Option</input><br>
<input type="radio" id="rad2" name="group" value="two">Second Option</input><br>
<input type="radio" id="rad3" name="group" value="three">Third Option</input><br>

<br>

<input type="radio" id="rad7" name="group2" value="one">First Option</input><br>
<input type="radio" id="rad8" name="group2" value="two">Second Option</input><br>
<input type="radio" id="rad9" name="group2" value="three">Third Option</input><br>

以下陈述是true

$('[value="one"]').length == 2

$('[name="group2"]').length == 3

那么怎么会..

$('[value="one"]',$('[name="group2"]')).length == 0

我本以为这应该有效地在所有具有 name==group2 的元素中查找具有 value=="one"1 的元素。为什么情况并非如此?

<input type="radio" id="rad1" name="group" value="one">First Option</input><br>
<input type="radio" id="rad2" name="group" value="two">Second Option</input><br>
<input type="radio" id="rad3" name="group" value="three">Third Option</input><br>

<br>

<input type="radio" id="rad7" name="group2" value="one">First Option</input><br>
<input type="radio" id="rad8" name="group2" value="two">Second Option</input><br>
<input type="radio" id="rad9" name="group2" value="three">Third Option</input><br>

The following statements are true

$('[value="one"]').length == 2

$('[name="group2"]').length == 3

So how come..

$('[value="one"]',$('[name="group2"]')).length == 0

?

I would have thought that this should effectively look within all elements with name==group2 for elements with value=="one" ie 1. Why is this not the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情深缘浅 2024-10-22 07:18:17

此选择器使用作用域/上下文的第二个参数:

$('[value="one"]', $('[name="group2"]'))

尝试查找具有 [value="one"]源自 具有 [name=" 的元素组2"]。这意味着您最终会向下查找 DOM 中至少两层不同的元素。

在您的情况下,您的单选按钮具有这两个属性,因此您想要附加两个属性选择器(我添加 input 只是为了节省 jQuery 的一些工作):

$('input[name="group2"][value="one"]')

This selector, using the second argument for scope/context:

$('[value="one"]', $('[name="group2"]'))

Attempts to find elements with [value="one"] that descend from elements with [name="group2"]. That means you end up looking down at least two levels of different elements in the DOM.

In your case, your radio buttons have both attributes, so you want to attach both attribute selectors instead (I add input just to save jQuery some work):

$('input[name="group2"][value="one"]')
剩余の解释 2024-10-22 07:18:17

我认为您想要的是:

$('[name="group2"][value="one"]')

这选择具有 name 属性且值为 group2 value 属性的元素值

$(selector, element) 是不同的。它将找到与 selector 匹配的 element 的所有后代。请参阅文档

I think what you want is:

$('[name="group2"][value="one"]')

This selects elements that have a name attribute with value group2 and value attribute with value one.

$(selector, element) is something different. It will find all descendants of element which match selector. See the documentation.

無心 2024-10-22 07:18:17
$('input:[value="one"], input:[name="group2"]').length === 5

您必须使用一个字符串,以逗号分隔。顺便说一句,编写像 [anything=foobar] 这样的选择器是不好的做法。这是通用选择器(*)的隐式用法,它将查询标记中的所有元素。

这将查询两个选择器并将结果添加到一个包装集中。如果您想查询拥有这两个属性的节点,请将其连接起来,如下所示:

$('input:[value="one"][name="group2"]').length === 1
$('input:[value="one"], input:[name="group2"]').length === 5

You have to use one string, comma separated. BTW it's bad practice to write a selector like [anything=foobar]. That is an implicit usage of the universal selector(*) which would query all elements in your markup.

This would query both selectors and add the results together in a wrapped set. If you want query for nodes which own both attributes, wire things up like:

$('input:[value="one"][name="group2"]').length === 1
新一帅帅 2024-10-22 07:18:17

您希望它从 group2 中查找值 one

$('input[name="group2"][value="one"]').length

这称为 多属性选择器(文档)

请注意,我还添加了一个 input 标签选择器。这样就不需要分析页面上的每个元素。

You want this instead to find value one from group2:

$('input[name="group2"][value="one"]').length

This is called the multiple-attribute-selector(docs).

Notice that I also added an input tag selector. This is so every element on the page doesn't need to be analyzed.

欢你一世 2024-10-22 07:18:17

尝试 $('[value="one"], [name="group2"]') 的长度

try the length of $('[value="one"], [name="group2"]')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文