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);
在IE下
selectid.options.remove(selectid.selectedIndex);
和
selectid.remove(selectid.selectedIndex); 都可以使用,
但是,在firefox下前者不认识,只是别后者。
}
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);
}
分享到:
相关推荐
js 操作select 下拉框js 操作select 下拉框
js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全
涵盖了js对select的常用操作 1.判断select选项中 是否存在Value="paraValue"的Item 2.向select选项中 加入一个Item 3.从select选项中 删除一个Item 4.修改select选项中 value="paraValue"的text为"paraText" 5.设置...
Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等
Js操作Select大全(取值、设置选中等等) Js操作Select大全(取值、设置选中等等)
js 操作select和optionjs 操作select和option
js操作select大全 js操作select大全 转
html最全的js javascript操作select 下拉框
JavaScript对Select的操作
对select的详细操作 1判断是否选中 2对属性的增删改成 3分类源码
JavaScript操作select JavaScript操作table
javascript 操作表单select的常用步骤,删除,添加,修改 用代码实现,一看就明白理解用法
Js操作Select大全.doc
Javascript操作Select和Option 一个网页 挺全的
JS对select动态添加options操作[IE&FireFox兼容]
Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等) JavaScript 1判断select选项中 是否存在Value="paraValue"的Item 2向select选项中 加入一个Item 3从select选项中 删除一个Item...
js_操作select_下拉框
里面提供 并解决所有获得select下拉框里面的值 很有帮助
今天在Ajax中使用两个下拉框动态添加select中的options,当我在IE测试下正常使用却在FireFox中不能正常使用,我对脚本也不是很熟悉,所以上网查了一下发现他们在动态添加option时有点不同。 下面把他记下来,说不定...