gpt4 book ai didi

jquery - 根据其文本而不是值选择下拉选项

转载 作者:行者123 更新时间:2023-12-01 04:09:56 25 4
gpt4 key购买 nike

我试图通过文本选择下拉列表中的一个项目。我无法按值选择它们,因为每次值都会不同。

   $("#rn_SelectionInput_7_attend_session_time option").each( function(){
alert($(this).text()==radioValue);// this evaluate to true when a matching item is found

if( $(this).text()==radioValue)
{
$("#rn_SelectionInput_7_attend_session_time").selectedIndex=$(this.index()); // this doesn't do anything
}
});

谢谢

最佳答案

可以设置选项元素的selected属性使其处于选中状态

$("#rn_SelectionInput_7_attend_session_time option").filter(function () {
alert($(this).text() == radioValue); // this evaluate to true when a matching item is found
return $.trim($(this).text()) == radioValue
}).prop('selected', true);

演示:Fiddle

  • filter() - 查找具有给定文本的选项元素
  • .prop() - 将所选属性设置为 true

关于jquery - 根据其文本而不是值选择下拉选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21924165/

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