1在select列表第一行增加一个option的方法
方法1.document.getElementById("sel").add(new Option("1","1"),0);
方法2var y=document.createElement('option');
y.text='2';
y.value='2';
document.getElementById("sel").add(y,0); //0为索引号
2在select列表第一行修改一个option的方法,注意是覆盖
document.getElementById('selectId').options[0] = new Option("1","1");
3在select列表删除一个option的方法
document.getElementById("sel").remove(0);//0为索引号
方法1.document.getElementById("sel").add(new Option("1","1"),0);
方法2var y=document.createElement('option');
y.text='2';
y.value='2';
document.getElementById("sel").add(y,0); //0为索引号
2在select列表第一行修改一个option的方法,注意是覆盖
document.getElementById('selectId').options[0] = new Option("1","1");
3在select列表删除一个option的方法
document.getElementById("sel").remove(0);//0为索引号
本文介绍了三种实用技巧:在Select列表中添加、修改及删除Option的方法。通过JavaScript操作DOM,实现动态更新Select选项,适用于网页表单等场景。
1745

被折叠的 条评论
为什么被折叠?



