var obj = document.getElementById("mySelect"); var index = obj.selectedIndex; obj.options[index] = new Option("three",3); //更改對應的值 obj.options[index].selected = true; //保持選取狀態
3.刪除所有選項option 複製程式碼複製程式碼複製程式碼複製程式碼複製程式碼複製程式碼複製碼🎜> 程式碼如下: function removeAll(){ var obj=document.getElementByIdx_x('mySelect'); obj.options.obj.options } 4.刪除一個選項option
function removeOne(){ var obj=document.getElementByIdx_x('mySelect'); //index,要刪除選項的序號,這裡取目前選取選項的序號 var index=obj .selectedIndex; obj.options.remove(index); }
複製碼> function removeSelect(){ var mySelect = document.getElementByIdx_x("mySelect"); mySelect.parentNode.removeChild(mySelect); } } } } } } 🎜>整個實例的完整程式碼如下: 複製程式碼 程式碼如下:
函數$(id) { return document.getElementByIdx_x(id) } function show() { var selectObj=$("area") var =document.createElement_x("option") myOption.setAttribute("value","10") myOption.appendChild(document.createTextNode("上海")) var myOption1= Element. "option") myOption1.setAttribute("value","100") myOption1.appendChild(document.createTextNode("南京")) selectObj.appendChild(myOption) } 函數choice() { var index=$("area").selectedIndex; var val=$("區域").options[index].getAttribute("value") if(val==10) { var i=$("context") .childNodes.length-1; var remobj=$("context").childNodes[i]; remobj.removeNode(true) var sh=document.createElement_x("select") sh.add(new Option("浦東新區","101")) sh.add( new Option("黃浦區","102")) sh.add(new Option("徐匯區","103")) sh.add(new Option("普陀區","104 ")) $("context").appendChild(sh) } if(val==100) { var i=$("context").childNodes.長度-1; var remobj=$("context").childNodes[i]; remobj.removeNode(true) var nj=document.createElement_x("select") nj.add(new Option("玄武區","201")) nj.add( new Option("白下區","202")) nj.add(new Option("下關區","203")) nj.add(new Option("棲霞區", "204")) $("context").appendChild(nj) } } function calc() { var x=$("context") .childNodes.length-1; alert(x) } 函式刪除() { var i=$("context").childNodes.length-1; var remobj=$("context").childNodes[i]; remobj.removeNode(true) } ;