[英]How to make <option>s of <select> show or hide using JavaScript?
我想通過程序(JS)進行<選擇>顯示或隱藏的選項,可以嗎?
就像StackOverFlow首頁中有趣的標簽一樣,當您鍵入一些單詞時,下拉列表將展開並提供建議。
ps。我知道在這種情況下StackOverFlow沒有使用select,無論如何都以它為例。
您可以從select
的options
集合中添加或刪除項目。
這是一個刪除一個項目並添加另一個項目的示例:
<html>
<head>
<title></title>
<script type="text/javascript">
function init() {
// get a reference to the element
var sel = document.getElementById('sel');
// remove an option
sel.options[2] = null;
// create a new option and add to the select
var opt = document.createElement('option');
opt.value = '5';
opt.text = 'five';
sel.options.add(opt);
}
</script>
</head>
<body onload="init();">
<form>
<select id="sel">
<option value="0">zero</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
</form>
</body>
</html>
function getCatListdef(sel)
{
var index = ajax.length;
ajax[index] = new sack();
ajax[index].requestFile = 'data.php?cat_code='+sel;
ajax[index].onCompletion = function(){ createCities(index) };
ajax[index].runAJAX();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.