日期:2014-05-17  浏览次数:20616 次

通过JavaScript操作HTML中select标签

很简单,代码如下:

添加:

?

function selectChange()
{
      var sel=document.getElementById("select1");
      Option  option = new Option("Text","Value");
      sel.add(option);
}
?

删除所有:

?

document.getElementById("select1").options.length=0;

?

删除单个Option元素:

?

var sel=document.getElementById("select1");
for(i=0;i<sel.options.length;i++)   
{   
     if(sel.options[i].selected)   
    {   
            sel.options[i]=null;       //设置为null就删除了这个元素
    }   
}  

?

取值:

?

var sel=document.getElementById("select1"):
var val=sel.options[sel.selectedIndex].value 
alert(val);  //得到Option的value 
var txt=sel.options[sel.selectedIndex].text 
alert(txt);  //得到Option的文本(即Text) 
?

?

?

?