使用 JQuery,如何引用同级的第一个选项
Using JQuery, how do I reference the first-option of a sibling?
我有兄弟姐妹 SELECT 菜单元素,这样......
<select name="country1" class="country">...</select>
<select name="state1" class="state">...</select>
名称将更改(例如"国家/地区 1"..."countryn"),但与元素关联的类(例如"country"和"state")不会。 在我的Javascript中,我想重写这一行...
var firstOption = $("#state option:first-child");
鉴于我有一个国家元素 $(countryElt),我想说,存储 $(countryElt) 的兄弟姐妹".state"元素的第一个选项。 我怎么写?
$('.state option:first')
.parent()
.siblings('.country')
.find('option:first');
嗨,戴夫,根据您的评论,我们最好在第二个选择中添加一个 ID,如下所示。
<select name="state1" class="state" id="state">...</select>.
所以代码将更改为
$('#state option:first')
.parent()
.siblings('.country')
.find('option:first');
您可以使用
parent()
和siblings()
,如下所示:
var stateFirstOption = $("#state option:first");
var countryFirstOption = $stateFirstOption.parent().siblings('.country').find('option:first');
你可以做:
$(countryElt).next("select.state option:first")
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- 如何为下拉列表的每个选项添加一个属性
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 我如何使第一个JS选项出现时,页面加载
- 如何将选项添加到淘汰js选项中的第一个
- 如何在jQuery中默认获取第一个HTML选项的值
- 动态选择选项列表重置为第一个值
- 如果选项卡是第一个,则禁用previous按钮
- 获取第一个h1标题,并在新选项卡中打开
h1.html - 避免使用jquery多次选择相同的选项,第一个select标记除外
- 更改第一个DropDownList的选项后,在Jquery中获取其他DropDownList的值