开发者

How do you set a selected item in an HTML dropdown by name, not by value?

I have an HTML combobox which contains unique options. For开发者_开发问答 example, a country dropdown which contains country_id as a value and country name as text.

Is there any way using jQuery or JavaScript to let me set the selected item using the country name not the country_id?


$("option:contains('COUNTRY NAME HERE')").attr('selected','selected');

something like that?


This links discusses some options: http://www.daftlogic.com/information-programmatically-preselect-dropdown-using-javascript.htm


You can do write like below:

$("option:selected").text() 

to obtain the text given for the option

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜