js操作select组件
来源:互联网 发布:汽车模拟改装软件 编辑:程序博客网 时间:2024/05/22 06:15
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" onclick="addAll()"/>
</p>
<p align="center">
<input name="add" type="button" id="add" style="width:40px" value=" > " onclick="add()"/>
</p>
<p align="center">
<input name="del" type="button" id="del" value=" < " style="width:40px" onclick="del()"/>
</p>
<p align="center">
<input name="delAll" type="button" id="delAll" value=" << " style="width:40px" onclick="delAll()"/>
</p></td>
<td width="22%"><select id="select2" size="15" multiple="multiple" style="width:100px">
</select></td>
</tr>
</table>
</body>
</html>
- js操作select组件
- js对select 操作
- js 操作select
- js 操作select标签
- js操作select控件
- js操作select控件
- js 操作Select标记
- js操作select
- js操作select控件
- js 操作select option
- js操作select
- js操作select
- JS操作select选中
- Js操作Select大全
- js操作select标签
- js操作select控件
- Js操作Select大全
- Js操作Select大全
- NS2如何在路由层获取队列的长度
- Teradata中四舍五入规则
- QT学习笔记-1
- 二叉搜索树头文件C语言(BinarySearchTree.h)
- 关于从Map.FeatureSelection获得selection不能读到Feature的其他属性问题
- js操作select组件
- 二叉搜索树实现文件C语言(BinarySearchTree.c)
- how to resolve the error message "Error reading Win32 manfest file"
- 几种同步手段(互斥量,信号量,事件,临界区)
- 一位朋友的几个购物网站,记一下,以防忘记。。
- 当时的撒
- 心酸。。。。
- 代码覆盖率浅谈
- 补好