繁体   English   中英

如何通过JavaScript获取<select>中当前选择的<option>?

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

如何通过JavaScript获取当前选择的<select>元素的<option>

这将为您做到:

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

select对象的.selectedIndex有一个索引; 您可以使用它来索引.options数组。

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

使用selectedOptions属性:

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

适用于除Internet Explorer之外的所有浏览器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM