selectedindex
Select selectedIndex 属性
定义和用法
selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。
注意: 若允许多重选择,则仅会返回第一个被选选项的索引号。
语法
设置 selectedIndex 属性:
selectObject.selectedIndex=integer
返回 selectedIndex 属性:
selectObject.selectedIndex
浏览器支持
所有主要浏览器都支持 selectedIndex 属性
实例
下面的例子可提示出被选选项的索引号:
<html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">Display index</button>
</body>
</html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">Display index</button>
</body>
</html>