jquery操作select元素和option的執行個體代碼,jqueryselect
廢話不多說了,直接給大家貼代碼,具體代碼如下所示:
<html><head> <title></title> <!--添加jquery--> <script src="../Script/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { createSelect("addSel"); addOption("addSel", "first", "第一個資料"); addOption("addSel", "secord", "第二個資料"); addOption("addSel", "three", "第三個資料"); addOption("addSel", "four", "第四個資料"); addOption("addSel", "fives", "第五個資料"); removeOneByIndex("addSel", 0); removeOneByValue("addSel", "three"); //****************以驗證不可以根據text值取得option元素*********************** //removeOneByText("addSel", "第三個資料"); //****************以驗證不可以根據text值取得option元素*********************** //removeAll("addSel"); //刪除select元素的所有options //removeSelect("addSel"); //刪除select元素; setDefaultByValue("addSel", "four"); //設定option的預設值 //添加一個option變更事件 調用自己寫的方法 $("#addSel").change(function () { alert("舊文本:" + getOptionText("addSel") + " 舊Value:" + getOptionValue("addSel")); editOptions("addSel", "新文本", "新Value"); //注意:不傳value值的時候 value值預設為text的值 alert("新文本:" + getOptionText("addSel") + " 新Value:" + getOptionValue("addSel")); }) }) //動態建立帶id的select function createSelect(id) { $("body").append("<select id="+id+"></select>"); } //根據select的id 添加選項option function addOption(selectID,value,text) { //根據id尋找select對象, var obj = $("#" + selectID + ""); $("<option></option>").val(value).text(text).appendTo(obj); } //根據value的值設定options預設選中項 function setDefaultByValue(selectID,value) { var obj = $("#" + selectID + ""); obj.val(value); } //獲得選中的Option Value; function getOptionValue(selectID) { //var obj = $("#" + selectID + " option:selected").val(); //上面和下面兩種都可以 var obj = $("#" + selectID + "").find("option:selected").val(); return obj; } //獲得選中的option Text; function getOptionText(selectID) { //var obj = $("#" + selectID + " option:selected").text(); //上面和下面兩種都可以 var obj = $("#" + selectID + "").find("option:selected").text(); return obj; } //修改選中的option function editOptions(selectID, newText, newValue) { var obj = $("#" + selectID + "").find("option:selected"); obj.val(newValue).text(newText); } //根據 index 值刪除一個選項option function removeOneByIndex(selectID, index) { var obj = $("#" + selectID + " option[index=" + index + "]"); obj.remove(); } //根據 value值刪除一個選項option function removeOneByValue(selectID, text) { var obj = $("#" + selectID + " option[value=" + text + "]"); obj.remove(); } //****************以驗證不可以根據text值取得option元素*********************** //根據text值刪除一個選項option 感覺不可用 真的 //function removeOneByText(selectID, text) { //var obj = $("#" + selectID + " option[text=" + text + "]"); //obj.remove(); //} //****************以驗證不可以根據text值取得option元素*********************** //刪除所有選項option function removeAll(selectID) { var obj = $("#" + selectID + ""); obj.empty(); } //刪除select function removeSelect(selectID){ var obj = $("#" + selectID + ""); obj.remove(); } </script></head><body></body></html>
以上所述是小編給大家分享的jquery操作select元素和option的執行個體代碼,希望對大家有所協助。
您可能感興趣的文章:
- 兩個select之間option的互相添加操作(jquery實現)
- jquery操作select option 的代碼小結
- JQuery操作Select的Options的Bug(IE8相容性檢視模式)
- jQuery解決下拉框select設寬度時IE 6/7/8下option超出顯示不全
- 淺析jQuery對select操作小結(遍曆option,操作option)
- 刪除select中所有option選項jquery代碼
- JQuery中對Select的option項的添加、刪除、取值
- jQuery操作Select的Option上下移動及移除添加等等