JavaScript操作select元素和option的執行個體代碼,selectoption

來源:互聯網
上載者:User

JavaScript操作select元素和option的執行個體代碼,selectoption

廢話不多說了,直接給大家貼代碼,具體代碼如下所示:

<!DOCTYPE html PUBLIC "-//WC//DTD XHTML . Transitional//EN" "http://www.w.org/TR/xhtml/DTD/xhtml-transitional.dtd"><html xmlns="http://www.w.org//xhtml"><head><title></title><!--添加jquery--><script src="../Script/jQuery/jquery-...min.js" type="text/javascript"></script><script type="text/javascript">$(function () {createSelect("select", "addSel");addOption("addSel", "first", "第一個資料");addOption("addSel", "secord", "第二個資料");addOption("addSel", "three", "第三個資料");addOption("addSel", "four", "第四個資料");addOption("addSel", "fives", "第五個資料");removeOneByIndex("addSel", );removeOneByObj("addSel", "secord");//添加一個option變更事件 調用自己寫的方法$("#addSel").change(function () {alert("舊文本:"+getOptionText("addSel") + "舊Value:" + getOptionValue("addSel"));editOptions("addSel", "新文本","新Value"); //注意:不傳value值的時候 value值預設為text的值alert("新文本:" + getOptionText("addSel") + "新Value:" + getOptionValue("addSel"));})})//動態建立帶id的元素function createSelect(element, id) {var select = document.createElement(element);select.id = id;document.body.appendChild(select);}//根據select的id 添加選項optionfunction addOption(selectID,value,text) {//根據id尋找對象, var obj = document.getElementById(selectID); obj.options.add(new Option(text, value)); //這個相容IE與firefox }//刪除所有選項optionfunction removeAll(selectID) {var obj = document.getElementById(selectID);obj.options.length = ;}//根據 index 值刪除一個選項optionfunction removeOneByIndex(selectID,index) {var obj = document.getElementById(selectID);//index,要刪除選項的序號,這裡取當前選中選項的序號 //var index = obj.selectedIndex;//擷取選中的選項的index;obj.options.remove(index);}//根據 value或者text值刪除一個選項optionfunction removeOneByObj(selectID, textOrValue) {var obj = document.getElementById(selectID);//index,要刪除選項的序號,這裡取當前選中選項的序號 //var index = obj.selectedIndex;//擷取選中的選項的index;for (var i = ; i < obj.options.length; i++) {if (obj.options[i].innerHTML == textOrValue || obj.options[i].value == textOrValue) {obj.options.remove(i);break;}}} //獲得一個Option Value;function getOptionValue(selectID){var obj = document.getElementById(selectID); var index=obj.selectedIndex; //序號,取當前選中選項的序號 var val = obj.options[index].value;return val;} //獲得一個option Text;function getOptionText(selectID) {var obj = document.getElementById(selectID); var index=obj.selectedIndex; //序號,取當前選中選項的序號 var val = obj.options[index].text;return val;}//修改選中的optionfunction editOptions(selectID,newText,newValue) {var obj = document.getElementById(selectID); var index=obj.selectedIndex; //序號,取當前選中選項的序號 obj.options[index] = new Option(newText, newValue);obj.options[index].selected = true;}//刪除selectfunction removeSelect(){var select = document.getElementById("select"); select.parentNode.removeChild(select); }</script></head><body></body></html>

以上所述是小編給大家分享的JavaScript操作select元素和option的執行個體代碼,希望對大家有所協助。

您可能感興趣的文章:
  • javascript據option的value值快速設定初始的selected選項
  • javascript Select標記中options操作方法集合
  • JavaScript Select和Option列表元素上下左右移動
  • javascript 動態設定已知select的option的value值的代碼
  • javascript select options 排序(保持option 對象完整性)
  • js 操作select和option常用代碼整理
  • javascript對select標籤的控制(option選項/select)
  • JS對select控制項option選項的增刪改查範例程式碼
  • js擷取select選中的option的text範例程式碼
  • js select option對象小結
  • JS動態添加與刪除select中的Option對象(範例程式碼)
  • JS擷取select-option-text_value的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.