读书人

透过JavaScript操作HTML中select标签

发布时间: 2012-10-08 19:54:56 作者: rapoo

通过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) 
?

?

?

?

读书人网 >CSS

热点推荐