javascript
通过JavaScript操作HTML中select标签
添加:
Js代碼
1.function selectChange()??
2.{??
3. var sel=document.getElementById("select1");??
4. Option option = new Option("Text","Value");??
5. sel.add(option);??
6.}??
function selectChange()
{
? var sel=document.getElementById("select1");
? Option option = new Option("Text","Value");
? sel.add(option);
}??
刪除所有:
Js代碼
1.document.getElementById("select1").options.length=0;??
? document.getElementById("select1").options.length=0;刪除單個Option元素:
Js代碼
1.var sel=document.getElementById("select1");??
2.for(i=0;i<sel.options.length;i++)??
3.{??
4. if(sel.options[i].selected)??
5. {??
6. sel.options[i]=null; //設置為null就刪除了這個元素??
7. }??
8.}??
var sel=document.getElementById("select1");
for(i=0;i<sel.options.length;i++)??
{??
? if(sel.options[i].selected)??
? {??
? sel.options[i]=null; //設置為null就刪除了這個元素
? }??
} 取值:
Js代碼
1.var sel=document.getElementById("select1"):??
2.var val=sel.options[sel.selectedIndex].value??
3.alert(val); //得到Option的value??
4.var txt=sel.options[sel.selectedIndex].text??
5.alert(txt); //得到Option的文本(即Text)??
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/JavaMan_KA/archive/2011/04/21/6339398.aspx
轉載于:https://www.cnblogs.com/ycxyyzw/archive/2011/06/07/2074045.html
總結
以上是生活随笔為你收集整理的通过JavaScript操作HTML中select标签的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用file做swap分区
- 下一篇: 根据mac地址查询厂商