该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
注意:Option中的O是要大写的,不然语法报错。
1.动态的创建select
//JavaScript
FunctioncreateSelect(){
mySelect = document.createElement(“select”);
mySelect.id=”mySelect”;
document.body.appendChild(mySelect);
}
//添加选项option
FunctionaddOption(){
var obj = document.getElementById(“mySelect”);
obj.add(new Option(“文本”,”值”)); //只在IE中有用
obj.options.add(new Option(“text”,”value”)); //兼容IE和firefox
}
拓展:
new Option(“text”,”value”,defaultselected,selected);
new Option(“文本1”,”值”,true,false);
2.删除所有的Option.
Function removeAll(){
var obj = document.getElementById(“mySelect”);
obj.options.length=0;
}
3.删除一个选项Option
Function removeOne(){
Var obj = document.getElementById(“mySelect”);
//index 要删除的选项的序号
Obj.options.remove(index);
}
4.获取选项Option的值
Var obj =document.getElementById(“mySelect”);
Var index =obj.options.selectedIndex;
Var val =obj.options[index].values;
5.获取选项Option的文本
Var obj =document.getElementById(“mySelect”);
Var index =obj.options.selectedIndex;
Var text =obj.options[index].text;
6.修改Option的值
Var obj =document.getElementById(“mySelect”);
Var index =obj.options.selectedIndex;
Var val =obj.options[index]=new Option(“新文本”,”新值”);
9.删除select
FunctionremoveSelect(){
Var mySelect = document.getElementById(“mySelect”);
mySelect.parentNode.removeChild(mySelect);
}