开发者

How to make <option>s of <select> show or hide using JavaScript?

I want to make the options of < select > show or hide by program(JS),is it possible?

It is just like the interesting tags in the front page of StackOverFlow, when you type in some words, the drop list will expand and give you suggestions.

ps.I know StackOverFlow didn't use select in this case, anyway just ta开发者_StackOverflow社区ke it as an example.


You add or remove items from the options collection of the select.

Here is an example that removes one item and adds another:

<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();    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜