Select selectedIndex 屬性
Select 對(duì)象定義和用法
selectedIndex 屬性可設(shè)置或返回下拉列表中被選選項(xiàng)的索引號(hào)。
注意: 若允許多重選擇,則僅會(huì)返回第一個(gè)被選選項(xiàng)的索引號(hào)。
語法
設(shè)置 selectedIndex 屬性:
selectObject.selectedIndex=integer
返回 selectedIndex 屬性:
selectObject.selectedIndex
瀏覽器支持
所有主要瀏覽器都支持 selectedIndex 屬性
實(shí)例
實(shí)例
下面的例子可提示出被選選項(xiàng)的索引號(hào):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool在線教程(w3cschool.cn)</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>W3Cschool在線教程(w3cschool.cn)</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 對(duì)象
更多建議: