How do you get the currently selected

2019-01-13 07:00发布

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

4条回答
做个烂人
2楼-- · 2019-01-13 07:35

Using the selectedOptions property:

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

It works in all browsers except Internet Explorer.

查看更多
贼婆χ
3楼-- · 2019-01-13 07:42

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

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-13 07:53
var payeeCountry = document.getElementById( "payeeCountry" );
alert( payeeCountry.options[ yourSelect.selectedIndex ].value );
查看更多
干净又极端
5楼-- · 2019-01-13 07:56

This will do it for you:

var yourSelect = document.getElementById( "your-select-id" );
alert( yourSelect.options[ yourSelect.selectedIndex ].value )
查看更多
登录 后发表回答