<form id="f">
<select size="1" name="s">
<option value="1">第一项</option>
<option value="2">第二项</option>
</select>
</form>
<script type="text/javascript" language="javascript">
<!--
var f = document.getElementById("f");
//列表项数目(有两种方法)
document.write(f.s.options.length);
document.write(f.s.length);
//当前选中项的下标(从 0 开始)(有两种方法)
//如果选择了多项,则返回第一个选中项的下标
document.write(f.s.options.selectedIndex);
document.write(f.s.selectedIndex);
//检测某一项是否被选中
document.write(f.s.options[0].selected);
//获得某一项的值和文字
document.write(f.s.options[0].value);
document.write(f.s.options[1].text);
//删除某一项
f.s.options[1] = null;
//追加一项
f.s.options[f.s.options.length] = new Option("追加的text", "追加的value");
//更改一项
f.s.options[1] = new Option("更改的text", "更改的value");
//也可以直接设置该项的 text 和 value
//-->
</script>
==================================================================
<select name="a">
<option value="">pls select</option>
<option value="1">aaaa1</option>
<option value="2">aaaa2</option>
<option value="3">aaaa3</option>
<option value="4">aaaa4</option>
</select>
<input type="button" οnclick="remove()" value="remove">
<input type="button" οnclick="add()" value="add">
<script language="javascript">
function remove(){
var obj = document.getElementById("a")
while(obj.length>1)
{
obj.options.remove(obj.length-1)
}
}
function add(){
var obj = document.getElementById("a")
for(i=1;i<5;i++)
{
var option = document.createElement("option")
obj.options.add(option)
obj.options[i].text = "aaaa"+i;
obj.options[i].value = i;
}
}
</script>
//以下是操作说明
1、往下拉框中添加选项:
var item = new Option(title,value);
document.all.ddlName.add(item);
2、删除下拉框中的某一项:
var obj = document.getElementById("ddlName");
obj.remove(obj.options[index]);
3、清空下拉框中的所有选项:
var obj = document.getElementById("ddlName");
for(var i = obj.options.length - 1; i > 0; i--) {
obj.remove(obj.options[index]);
}
//另一种方法,直接设置长度为0,不过好像只有IE支持这种写法
document.all.ddlName.options.length = 0;
4、获取下拉框当前选中的值:
document.all.ddlName.value;