开发者

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

开发者 https://www.devze.com 2023-01-06 21:25 出处:网络
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.

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

精彩评论

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