定义和用法
selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。
注释:若允许多重选择,则仅会返回第一个被选选项的索引号。
语法
selectObject.selectedIndex=number
下面的例子可提示出被选选项的索引号:
选中第一个:document.getElementById('xxx').selectedIndex = 0;
selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。
注释:若允许多重选择,则仅会返回第一个被选选项的索引号。
语法
selectObject.selectedIndex=number
下面的例子可提示出被选选项的索引号:
<html>
<head>
<script type="text/javascript">
function getIndex()
{
var x=document.getElementById("mySelect")
alert(x.selectedIndex)
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getIndex()"
value="Alert index of selected option">
</form>
</body>
</html>
选中第一个:document.getElementById('xxx').selectedIndex = 0;
本文介绍了HTML中selectedIndex属性的定义和使用方法。此属性用于获取或设置下拉列表中被选选项的索引号,并通过一个示例展示了如何在JavaScript中利用该属性来获取当前选中的选项索引。
2569

被折叠的 条评论
为什么被折叠?



