[英]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.