Latest web development tutorials

HTML DOM Select remove() 方法

Select 對象參考手冊 Select對象

定義和用法

remove() 方法用於從下拉列表刪除選項。

語法

selectObject.remove(index)

参数 描述
index 必需。规定要删除的选项的索引号。


瀏覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要瀏覽器都支持remove() 方法


實例

實例

下面的例子可從列表中刪除被選的選項:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function removeOption(){
var x=document.getElementById("mySelect");
x.remove(x.selectedIndex);
}
</script>
</head>
<body>

<form>
<select id="mySelect">
<option>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>
<input type="button" onclick="removeOption()" value="移除選項">
</form>

</body>
</html>

嘗試一下»


實例

更多實例

從下拉列表中刪除最後的選項


Select 對象參考手冊 Select對象