Jquery 根据其文本而非值选择下拉选项
我试图通过文本选择下拉列表项。我不能按值选择它们,因为每次值都不同Jquery 根据其文本而非值选择下拉选项,jquery,Jquery,我试图通过文本选择下拉列表项。我不能按值选择它们,因为每次值都不同 $("#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 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
}
});
谢谢,您可以将option元素的selected属性设置为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);
演示:
- -查找具有给定文本的选项元素
- -将所选属性设置为true
.attr()
,如果jQuery>=1.6.4,那么.prop()