gpt4 book ai didi

javascript - 使用 JQuery,如何引用同级的第一个选项?

转载 作者:行者123 更新时间:2023-12-03 11:03:23 25 4
gpt4 key购买 nike

我有同级 SELECT 菜单元素,如下所示...

<select name="country1" class="country">...</select>
<select name="state1" class="state">...</select>

名称将会更改(例如“country1”...“countryn”),但与元素关联的类(例如“country”和“state”)不会更改。在我的 Javascript 中,我想重写这一行...

var firstOption = $("#state option:first-child");

假设我有一个国家/地区元素 $(countryElt),并且我想说,存储 $(countryElt) 的同级“.state”元素的第一个选项。我该怎么写呢?

最佳答案

$('.state option:first')
.parent()
.siblings('.country')
.find('option:first');

HI Dave,根据您的意见,我们最好在第二个选择中添加一个 ID,如下所示。

<select name="state1" class="state" id="state">...</select>. 

所以代码改为

$('#state option:first')
.parent()
.siblings('.country')
.find('option:first');

关于javascript - 使用 JQuery,如何引用同级的第一个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27974082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com