網(LieHuo.Net)教程 項目中碰到需要用javascript操作下拉框的情況,順便做一下總結,列出一些常用方法,以下方法均在FIRFOX3.5及IE8上測試過,如有其他瀏覽器無法正常運行的請與筆者聯系。
以下為引用的內容: //添加一個下拉框 function AddDropDownList(id,fatherCtl) { if(!document.getElementById(id)) { var ddl = document.createElement('select'); ddl.setAttribute("id",id); if(fatherCtl&&document.getElementById(fatherCtl)) document.getElementById(fatherCtl).appendChild(ddl); else document.body.appendChild(ddl); } } //刪除指定的下拉框 function RemoveDropDownList(id) { var ctl = document.getElementById(id); if(ctl) ctl.parentNode.removeChild(ctl); } //給下拉框添加選項 function AddDDDLOption(id,text,value) { var ctl = document.getElementById(id); if(ctl) { ctl.options[ctl.options.length] = new Option(text,value); } } //刪除所有選項 function RemoveAllDDLOptions(id) { var ctl = document.getElementById(id); if(ctl) { ctl.options.length=0; } } //刪除指定索引的選項 function RemoveDDLOption(id,index) { var ctl=document.getElementById(id); if(ctl && ctl.options[index]) { ctl.options[index]=null; } } //獲取下拉框選擇的值 function GetDDLSelectedValue(id) { var ctl = document.getElementById(id); if(ctl) { return ctl.options[ctl.selectedIndex].value; } } //獲取下拉框選擇的文本 function GetDDLSelectedText(id) { var ctl = document.getElementById(id); if(ctl) { return ctl.options[ctl.selectedIndex].text; } } |
文章來自:http://www.cnblogs.com/liuwu/ 作者:劉武