使用 jquery 在多组下拉列表中选择第一个选项
我的下拉菜单与此非常相似:
<select id='someSelect'>
<option value="0">---select one---</option>
<optgroup label="Bikes">
<option value="B-4">Hayabusa</option>
<option value="B-2">GSXR</option>
<option value="B-3">Ninja</option>
<option value="B-6">Enticer</option>
</optgroup>
<optgroup label="Cars">
<option value="C-4">Audi TT</option>
<option value="C-2">Awesome Car</option>
<option value="C-23">Japanese car</option>
<option value="C-9">German car</option>
</optgroup>
</select>
我只想选择第一组的第一个元素(此处为自行车)。请问我该如何在 jQuery 中进行操作?
目前,我尝试了这个:
$('#someSelect option:nth-child(1)').attr("selected", "selected");
但是,问题是,因为有三个第一个元素(--select--
,Hayabusa
和奥迪 TT
)它选择了所有三个,最终选择了奥迪TT
我尝试对每个
做一些事情并只选择第二个,但后来我意识到下拉菜单是动态的,我不想选择默认的(即--select one--
),但第一组的第一个元素
我试图模拟一个jsfiddle,但它是搞砸了并且无法工作,不知道为什么:-/
你可以在这里看到
I have dropdown very similar to this:
<select id='someSelect'>
<option value="0">---select one---</option>
<optgroup label="Bikes">
<option value="B-4">Hayabusa</option>
<option value="B-2">GSXR</option>
<option value="B-3">Ninja</option>
<option value="B-6">Enticer</option>
</optgroup>
<optgroup label="Cars">
<option value="C-4">Audi TT</option>
<option value="C-2">Awesome Car</option>
<option value="C-23">Japanese car</option>
<option value="C-9">German car</option>
</optgroup>
</select>
I just want to select the 1st element of 1st group (bikes here). How do I go about it in jQuery please?
Currently, I tried this:
$('#someSelect option:nth-child(1)').attr("selected", "selected");
BUT, the trouble is, since there are three 1st elements ( --select--
, Hayabusa
and Audi TT
) it selects all three, which finaly selects Audi TT
I tried to do some stuff with each
and select just the second one, but then I realized that the dropdown is dynamic, I don't want to select the default one (which is --select one--
) but the first element of first group
I tried to mock up a jsfiddle, but it's mucked up and not working, not sure why :-/
you can see it here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里是一个示例,这是我使用的选择器:
如您所见,我使用了第一个选项通过查看 optgroup 元素内部。
Here is an example and here is the selector I used:
As you can see, I used the first option by looking inside the optgroup element.
嗯,这有效:
http://jsfiddle.net/nYd67/1/
Well this works:
http://jsfiddle.net/nYd67/1/
从 optgroup 中选择而不是选择:
或者,如果您不想指定标签,也只需在 optgroup 上进行过滤:
select from the optgroup instead of the select:
Or, if you don't want to specify the label, just filter on the optgroup as well:
只需在选择器中包含 optgroup 即可:
只需记住
:nth-child()
选择器是从 1 开始的,而不是从 0 开始的。另外,在这种情况下,您甚至不需要使用标签名称来限定选择器,因此它也可以是:Just include the optgroup in your selector:
Just remember that the
:nth-child()
selector is 1-based, not 0-based. Also, in this case, you don't even need to qualify the selector with a tag name, so it could also be just:我总是发现
.eq()
更容易使用。这似乎在你的 jsfiddle 中工作正常。I always find that
.eq()
is a lot easier to use. This seems to work correctly in your jsfiddle.这有效,我已经在你的 html 上进行了测试
this works, I have tested on your html
id 为“someSelect”的
select
元素的第一个optgroup
中第一个option
的选择器:The selector for the first
option
in the firstoptgroup
of theselect
element with an id of "someSelect":