定義和用法
selectedIndex 屬性可設置或返回下拉列表中被選選項的索引號。
注意: 若允許多重選擇,則僅會返回第一個被選選項的索引號。
語法
設置 selectedIndex 屬性:
返回 selectedIndex 屬性:
瀏覽器支持
所有主要瀏覽器都支持 selectedIndex 屬性
實例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
<script>
function displayResult(){
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("索引: " + y[x].index + " is " + y[x].text);
}
</script>
</head>
<body>
<form>
選擇你最喜歡的水果:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">顯示索引</button>
</body>
</html>
來自:http://www.runoob.com/jsref/prop-select-selectedindex.html