Select selectedIndex 属性
Select 对象定义和用法
selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。
注意: 若允许多重选择,则仅会返回第一个被选选项的索引号。
语法
设置 selectedIndex 属性:
selectObject.selectedIndex=integer
返回 selectedIndex 属性:
selectObject.selectedIndex
浏览器支持
所有主要浏览器都支持 selectedIndex 属性
实例
实例
下面的例子可提示出被选选项的索引号:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>悠悠之家(uoften.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>悠悠之家(uoften.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 对象