簡體   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