jquery及js动态添加删除option示例

来源:互联网 发布:什么是淘宝死店 编辑:程序博客网 时间:2024/06/05 14:30

js动态添加:

var sel= document.getElementById("selectId"); 

sel.options.add(new Option("请选择",""));

sel.options.add(new Option("name","id"));


js动态动态删除所有option:

document.getElementById("selectId").length=0;


js删除一个选项option:

var obj=document.getElementById('mySelect');

varindex=obj.selectedIndex;//序号,取当前选中选项的序号

var val = obj.options[index].value; //Option值

var val = obj.options[index].text; //option文本

varval = obj.options[index]=new Option("新文本","新值"); //修改当前option

functionremoveOne(){

var obj=document.getElementById('selectId');//index,要删除选项的序号,这里取当前选中选项的序号

var index=obj.selectedIndex;

obj.options.remove(index);

}


js删除select

functionremoveSelect(){

var mySelect = document.getElementById("selectId");

mySelect.parentNode.removeChild(mySelect);

}


jquery动态添加option:

$("#selectId").append("<option value='"+value+"'>"+text+"</option>");


jquery动态删除所有option:

$("#selectedId option").remove();


原创粉丝点击