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