1.动态创建select
function createSelect(){
var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}
2.添加选项option
function addOption(){
//根据id查找对象,
var obj=document.getElementById('mySelect');
//添加一个选项
obj.add(new Option("文本","值")); //这个只能在IE中有效
obj.options.add(new Option("text","value")); //这个兼容IE与firefox
}
3.删除所有选项option
function removeAll(){
var obj=document.getElementById('mySelect');
obj.options.length=0;
}
4.删除一个选项option
function removeOne(){
var obj=document.getElementById('mySelect');
//index,要删除选项的序号,这里取当前选中选项的序号
var index=obj.selectedIndex;
obj.options.remove(index);
}
5.获得选项option的值
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].value;
6.获得选项option的文本
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index].text;
7.修改选项option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序号,取当前选中选项的序号
var val = obj.options[index]=new Option("新文本","新值");
8.删除select
function removeSelect(){
var mySelect = document.getElementById("mySelect");
mySelect.parentNode.removeChild(mySelect);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script language="javascript">
function add(){
var opt=selectfrom.options;
len=opt.length;
for(i=len-1;i>-1;i--){
if(opt[i].selected){
select2.appendChild(opt[i]);
}
}
}
function addAll(){
var opt=selectfrom.options;
len=opt.length;
for(i=len-1;i>-1;i--){
select2.appendChild(opt[i]);
}
}
function del(){
var opt=select2.options;
len=opt.length;
for(i=len-1;i>-1;i--){
if(opt[i].selected){
selectfrom.appendChild(opt[i]);
}
}
}
function delAll(){
var opt=select2.options;
len=opt.length;
for(i=len-1;i>-1;i--){
selectfrom.appendChild(opt[i]);
}
}
</script>
<body>
<table width="27%" height="194" border="0" cellpadding="1" cellspacing="0">
<tr>
<td width="21%"><select id="selectfrom" size="15" multiple="multiple" style="width:100px">
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
<option>e</option>
<option>f</option>
<option>g</option>
<option>h</option>
<option>i</option>
<option>j</option>
<option>k</option>
<option>j</option>
<option>m</option>
<option>n</option>
</select></td>
<td width="57%"><p align="center">
<input name="addAll" type="button" id="addAll" value=" >> " style="width:40px" οnclick="addAll()"/>
</p>
<p align="center">
<input name="add" type="button" id="add" style="width:40px" value=" > " οnclick="add()"/>
</p>
<p align="center">
<input name="del" type="button" id="del" value=" < " style="width:40px" οnclick="del()"/>
</p>
<p align="center">
<input name="delAll" type="button" id="delAll" value=" << " style="width:40px" οnclick="delAll()"/>
</p></td>
<td width="22%"><select id="select2" size="15" multiple="multiple" style="width:100px">
</select></td>
</tr>
</table>
</body>
</html>