Latest web development tutorials

HTML DOM Select options 集合

Select 對象參考手冊 Select對象

定義和用法

option 集合可返回包含<select> 元素中所有<option> 的一個數組。

注意:數組中的每個元素對應一個<option>標籤-由0起始。

語法

selectObject.options

屬性

属性 描述
length 返回集合的option元素数目
selectedIndex 设置或者返回select对象已选选项的索引值。(以 0 起始)

方法

方法 描述
[index] 以数字形式指定元素索引 (以 0 开始)
[add(element[,index])] 在集合中添加option元素
item(index) 以数字索引返回集合中元素
namedItem(name) 以名称为索引返回集合元素
remove(index) 从集合中移除元素


瀏覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要瀏覽器都支持options 集合


實例

實例

循環輸出下拉列表中的所有選項:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
var x=document.getElementById("mySelect");
var txt="All options: ";
var i;
for (i=0;i<x.length;i++){
txt=txt + "\n" + x.options[i].text;
}
alert(txt);
}
</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>

嘗試一下»


實例s

更多實例

通過另外一個下拉菜單已選選項來修改當前下拉菜單的選項。


Select 對象參考手冊 Select對象