透過 selectedIndex 索引取得被選中的 <option>,再分別讀取 value 與 text 屬性,相容 IE、Firefox、Chrome、Opera 等主流瀏覽器。
var selectEl = document.getElementById("select");
var value = selectEl.options[selectEl.selectedIndex].value;
var text = selectEl.options[selectEl.selectedIndex].text;
<script>
function check() {
var sel = document.getElementById("select");
var val = sel.options[sel.selectedIndex].value;
alert("選中的值:" + val);
}
</script>
<select name="select" id="select">
<option value="test1" selected>Test1</option>
<option value="test2">Test2</option>
</select>
<input type="button" value="我要試試" onclick="check()" />
multiple),請改用迴圈遍歷 options 檢查 selected 屬性。querySelector 或 jQuery val() 簡化操作。