<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 42, 文章 - 1, 評(píng)論 - 0, 引用 - 0
    數(shù)據(jù)加載中……

    Jquery操作select

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

    1.獲取列表項(xiàng)中候選項(xiàng)的數(shù)目。

    2.獲得選中項(xiàng)的索引值。

    3.獲得當(dāng)前選中項(xiàng)的值

    4.設(shè)定選擇值

    5.設(shè)定選擇項(xiàng)


     1 //得到select項(xiàng)的個(gè)數(shù)   
      2 jQuery.fn.size = function(){   
      3     return jQuery(this).get(0).options.length;   
      4 }   
      5 
      6 //獲得選中項(xiàng)的索引   
      7 jQuery.fn.getSelectedIndex = function(){   
      8     return jQuery(this).get(0).selectedIndex;   
      9 }   
     10 
     11 //獲得當(dāng)前選中項(xiàng)的文本   
     12 jQuery.fn.getSelectedText = function(){   
     13     if(this.size() == 0return "下拉框中無(wú)選項(xiàng)";   
     14     else{   
     15         var index = this.getSelectedIndex();         
     16         return jQuery(this).get(0).options[index].text;   
     17     }   
     18 }   
     19 
     20 //獲得當(dāng)前選中項(xiàng)的值   
     21 jQuery.fn.getSelectedValue = function(){   
     22     if(this.size() == 0)    
     23         return "下拉框中無(wú)選中值";   
     24        
     25     else 
     26         return jQuery(this).val();   
     27 }   
     28 
     29 //設(shè)置select中值為value的項(xiàng)為選中   
     30 jQuery.fn.setSelectedValue = function(value){   
     31     jQuery(this).get(0).value = value;   
     32 }   
     33 
     34 //設(shè)置select中文本為text的第一項(xiàng)被選中   
     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("下拉框中不存在該項(xiàng)");   
     51     }   
     52 }   
     53 //設(shè)置選中指定索引項(xiàng)   
     54 jQuery.fn.setSelectedIndex = function(index)   
     55 {   
     56     var count = this.size();       
     57     if(index >= count || index < 0)   
     58     {   
     59         alert("選中項(xiàng)索引超出范圍");   
     60     }   
     61     else 
     62     {   
     63         jQuery(this).get(0).selectedIndex = index;   
     64     }   
     65 }   
     66 //判斷select項(xiàng)中是否存在值為value的項(xiàng)   
     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中添加一項(xiàng),顯示內(nèi)容為text,值為value,如果該項(xiàng)值已存在,則提示   
     82 jQuery.fn.addOption = function(text,value)   
     83 {   
     84     if(this.isExistItem(value))   
     85     {   
     86         alert("待添加項(xiàng)的值已存在");   
     87     }   
     88     else 
     89     {   
     90         jQuery(this).get(0).options.add(new Option(text,value));   
     91     }   
     92 }   
     93 //刪除select中值為value的項(xiàng),如果該項(xiàng)不存在,則提示   
     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("待刪除的項(xiàng)不存在!");   
    111     }   
    112 }   
    113 //刪除select中指定索引的項(xiàng)   
    114 jQuery.fn.removeIndex = function(index)   
    115 {   
    116     var count = this.size();   
    117     if(index >= count || index < 0)   
    118     {   
    119         alert("待刪除項(xiàng)索引超出范圍");   
    120     }   
    121     else 
    122     {   
    123         jQuery(this).get(0).remove(index);   
    124     }   
    125 }   
    126 //刪除select中選定的項(xiàng)   
    127 jQuery.fn.removeSelected = function()   
    128 {   
    129     var index = this.getSelectedIndex();   
    130     this.removeIndex(index);   
    131 }   
    132 //清除select中的所有項(xiàng)   
    133 jQuery.fn.clearAll = function()   
    134 {   
    135     jQuery(this).get(0).options.length = 0;   
    136 

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

    主站蜘蛛池模板: 无码人妻一区二区三区免费看 | 国产精品自在自线免费观看| 国产亚洲一区二区手机在线观看 | 亚洲国产婷婷综合在线精品| 亚洲成人午夜电影| 成全高清在线观看免费| 国产精品亚洲视频| 一个人看的www免费高清| 成人片黄网站A毛片免费| 国产精品亚洲片在线观看不卡| 久久久国产亚洲精品| 无码国产精品一区二区免费| 亚洲国产一区国产亚洲| 久9这里精品免费视频| 国产成人免费福利网站| 亚洲变态另类一区二区三区| 久久久久久久91精品免费观看| 国产亚洲sss在线播放| 国产成人免费片在线视频观看| 久青草国产免费观看| 亚洲AV无码一区二区乱孑伦AS| 免费黄色网址网站| 国产精品亚洲专区无码WEB| 亚洲日本韩国在线| 日韩在线永久免费播放| 亚洲综合校园春色| 亚洲成a人片在线观看久| 东方aⅴ免费观看久久av| 亚洲午夜电影在线观看| 免费一级e一片在线播放| 无码日韩精品一区二区三区免费 | 日本亚洲色大成网站www久久| 国产精品美女自在线观看免费| 亚洲一区二区三区免费| 亚洲视频在线免费播放| 韩国日本好看电影免费看| 在线观看人成视频免费无遮挡| 亚洲毛片免费观看| 亚洲国产精品激情在线观看| 99在线视频免费| 男女交性无遮挡免费视频|