js控制select的option
JS select里添加一条OPTION
<select id="abc">
<option></option>
</select>
<input type="button" name="button" value="按钮" οnclick="dd();">
<script>
function dd(){
var cc = document.all["abc"];
//方法一:直接select 的最下面增加一条OPTION
cc.options.add(new Option('VALUE','key'));
//方法二:可以用 len 来控制OPTION的插入位置
//var len = cc.length;
//cc.options[len] = new Option('VALUE','key');
}
</script>
动态删除select中的所有options:
document.getElementById("ddlResourceType").options.length=0;
动态删除select中的某一项option:
document.getElementById("ddlResourceType").options.remove(indx);
//就是这句不兼容了,Firefox是不懂 remove 这个方法的,所以会报错了,当然也移除不了了 动态添加select中的项option:
document.getElementById("ddlResourceType").options.add(new Option(text,value));
取值方面
function getvalue(obj)
{
var m=obj.options[obj.selectedIndex].value
alert(m);//获取value
var n=obj.options[obj.selectedIndex].text
alert(n);//获取文本
}
<option></option>
</select>
<input type="button" name="button" value="按钮" οnclick="dd();">
<script>
function dd(){
var cc = document.all["abc"];
//方法一:直接select 的最下面增加一条OPTION
cc.options.add(new Option('VALUE','key'));
//方法二:可以用 len 来控制OPTION的插入位置
//var len = cc.length;
//cc.options[len] = new Option('VALUE','key');
}
</script>
动态删除select中的所有options:
document.getElementById("ddlResourceType").options.length=0;
动态删除select中的某一项option:
document.getElementById("ddlResourceType").options.remove(indx);
//就是这句不兼容了,Firefox是不懂 remove 这个方法的,所以会报错了,当然也移除不了了 动态添加select中的项option:
document.getElementById("ddlResourceType").options.add(new Option(text,value));
取值方面
function getvalue(obj)
{
var m=obj.options[obj.selectedIndex].value
alert(m);//获取value
var n=obj.options[obj.selectedIndex].text
alert(n);//获取文本
}
js删除select option
- for(i=0;i<templateId.options.length;i++)
- {
- if(templateId.options[i].selected)
- {
- templateId.options[i]=null;//付null即删除
- }
- }