开发者

How do you get the currently selected <option> in a <select> via JavaScript?

开发者 https://www.devze.com 2023-01-08 11:59 出处:网络
How do you get the currently selected <option> of a <select> element via JavaS开发者_高级运维cript?This will do it for you:

How do you get the currently selected <option> of a <select> element via JavaS开发者_高级运维cript?


This will do it for you:

var yourSelect = document.getElementById( "your-select-id" );
alert( yourSelect.options[ yourSelect.selectedIndex ].value )


The .selectedIndex of the select object has an index; you can use that to index into the .options array.


var payeeCountry = document.getElementById( "payeeCountry" );
alert( payeeCountry.options[ payeeCountry.selectedIndex ].value );


Using the selectedOptions property:

var yourSelect = document.getElementById("your-select-id");
alert(yourSelect.selectedOptions[0].value);

It works in all browsers except Internet Explorer.

0

精彩评论

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