HTML DOM Select selectedIndex 屬性
定義和用法
selectedIndex 屬性可設置或返回下拉列表中被選選項的索引號。
注意:若允許多重選擇,則僅會返回第一個被選選項的索引號。
語法
設置selectedIndex 屬性:
selectObject.selectedIndex=integer
返回selectedIndex 屬性:
selectObject.selectedIndex
瀏覽器支持
所有主要瀏覽器都支持selectedIndex 屬性
實例
實例
下面的例子可提示出被選選項的索引號:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.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>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.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>
嘗試一下»
Select對象