隨筆 - 42, 文章 - 1, 評論 - 0, 引用 - 0
          數據加載中……

          Jquery操作select

          以下是操作 下拉列表的常用功能:

          1.獲取列表項中候選項的數目。

          2.獲得選中項的索引值。

          3.獲得當前選中項的值

          4.設定選擇值

          5.設定選擇項


           1 //得到select項的個數   
            2 jQuery.fn.size = function(){   
            3     return jQuery(this).get(0).options.length;   
            4 }   
            5 
            6 //獲得選中項的索引   
            7 jQuery.fn.getSelectedIndex = function(){   
            8     return jQuery(this).get(0).selectedIndex;   
            9 }   
           10 
           11 //獲得當前選中項的文本   
           12 jQuery.fn.getSelectedText = function(){   
           13     if(this.size() == 0return "下拉框中無選項";   
           14     else{   
           15         var index = this.getSelectedIndex();         
           16         return jQuery(this).get(0).options[index].text;   
           17     }   
           18 }   
           19 
           20 //獲得當前選中項的值   
           21 jQuery.fn.getSelectedValue = function(){   
           22     if(this.size() == 0)    
           23         return "下拉框中無選中值";   
           24        
           25     else 
           26         return jQuery(this).val();   
           27 }   
           28 
           29 //設置select中值為value的項為選中   
           30 jQuery.fn.setSelectedValue = function(value){   
           31     jQuery(this).get(0).value = value;   
           32 }   
           33 
           34 //設置select中文本為text的第一項被選中   
           35 jQuery.fn.setSelectedText = function(text)   
           36 {   
           37     var isExist = false;   
           38     var count = this.size();   
           39     for(var i=0;i<count;i++)   
           40     {   
           41         if(jQuery(this).get(0).options[i].text == text)   
           42         {   
           43             jQuery(this).get(0).options[i].selected = true;   
           44             isExist = true;   
           45             break;   
           46         }   
           47     }   
           48     if(!isExist)   
           49     {   
           50         alert("下拉框中不存在該項");   
           51     }   
           52 }   
           53 //設置選中指定索引項   
           54 jQuery.fn.setSelectedIndex = function(index)   
           55 {   
           56     var count = this.size();       
           57     if(index >= count || index < 0)   
           58     {   
           59         alert("選中項索引超出范圍");   
           60     }   
           61     else 
           62     {   
           63         jQuery(this).get(0).selectedIndex = index;   
           64     }   
           65 }   
           66 //判斷select項中是否存在值為value的項   
           67 jQuery.fn.isExistItem = function(value)   
           68 {   
           69     var isExist = false;   
           70     var count = this.size();   
           71     for(var i=0;i<count;i++)   
           72     {   
           73         if(jQuery(this).get(0).options[i].value == value)   
           74         {   
           75             isExist = true;   
           76             break;   
           77         }   
           78     }   
           79     return isExist;   
           80 }   
           81 //向select中添加一項,顯示內容為text,值為value,如果該項值已存在,則提示   
           82 jQuery.fn.addOption = function(text,value)   
           83 {   
           84     if(this.isExistItem(value))   
           85     {   
           86         alert("待添加項的值已存在");   
           87     }   
           88     else 
           89     {   
           90         jQuery(this).get(0).options.add(new Option(text,value));   
           91     }   
           92 }   
           93 //刪除select中值為value的項,如果該項不存在,則提示   
           94 jQuery.fn.removeItem = function(value)   
           95 {       
           96     if(this.isExistItem(value))   
           97     {   
           98         var count = this.size();           
           99         for(var i=0;i<count;i++)   
          100         {   
          101             if(jQuery(this).get(0).options[i].value == value)   
          102             {   
          103                 jQuery(this).get(0).remove(i);   
          104                 break;   
          105             }   
          106         }           
          107     }   
          108     else 
          109     {   
          110         alert("待刪除的項不存在!");   
          111     }   
          112 }   
          113 //刪除select中指定索引的項   
          114 jQuery.fn.removeIndex = function(index)   
          115 {   
          116     var count = this.size();   
          117     if(index >= count || index < 0)   
          118     {   
          119         alert("待刪除項索引超出范圍");   
          120     }   
          121     else 
          122     {   
          123         jQuery(this).get(0).remove(index);   
          124     }   
          125 }   
          126 //刪除select中選定的項   
          127 jQuery.fn.removeSelected = function()   
          128 {   
          129     var index = this.getSelectedIndex();   
          130     this.removeIndex(index);   
          131 }   
          132 //清除select中的所有項   
          133 jQuery.fn.clearAll = function()   
          134 {   
          135     jQuery(this).get(0).options.length = 0;   
          136 

          posted on 2012-04-05 14:00 段旭 閱讀(1147) 評論(0)  編輯  收藏 所屬分類: JQuery

          主站蜘蛛池模板: 博湖县| 饶平县| 新民市| 永寿县| 蒙城县| 洛阳市| 苏州市| 刚察县| 北安市| 章丘市| 南部县| 泌阳县| 绥宁县| 郁南县| 临安市| 德格县| 孟津县| 杂多县| 营山县| 东宁县| 木里| 马尔康县| 荣成市| 宝清县| 阿勒泰市| 白朗县| 进贤县| 互助| 旺苍县| 临猗县| 新龙县| 客服| 田阳县| 综艺| 三门县| 聊城市| 安远县| 松阳县| 大庆市| 通化县| 锡林浩特市|