<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 "下拉框中無選項(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 "下拉框中無選中值";   
     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 段旭 閱讀(1149) 評(píng)論(0)  編輯  收藏 所屬分類: JQuery

    主站蜘蛛池模板: 亚洲天堂中文字幕在线观看| 亚洲精品视频在线| 久久精品国产亚洲av瑜伽| 免费看成人AA片无码视频羞羞网| 亚洲天堂中文字幕| 午夜视频免费在线观看| 亚洲视频在线视频| 18禁黄网站禁片免费观看不卡| 亚洲色欲色欲综合网站| 亚洲免费二区三区| 亚洲国产模特在线播放| 成人免费视频77777| 亚洲成aⅴ人片久青草影院按摩| 国产zzjjzzjj视频全免费| 亚洲GV天堂无码男同在线观看| 爽爽日本在线视频免费| 久久影视国产亚洲| 好男人资源在线WWW免费| 亚洲欧洲无码AV电影在线观看| 黄页免费在线观看| 亚洲伊人色一综合网| 免费无码看av的网站| 一二三四在线观看免费中文在线观看 | 黄网站色视频免费观看45分钟| 亚洲av日韩av欧v在线天堂| g0g0人体全免费高清大胆视频| 韩国免费一级成人毛片| 亚洲精品无AMM毛片| 九月婷婷亚洲综合在线| 成人久久免费网站| 国产成人精品日本亚洲专区6| 国产美女无遮挡免费视频网站| a级毛片免费观看网站| 18亚洲男同志videos网站| 精品少妇人妻AV免费久久洗澡| 成人免费网站视频www| 亚洲专区先锋影音| 国产精品免费视频播放器| 99久久精品毛片免费播放| 亚洲综合av一区二区三区不卡| 国产精品亚洲综合一区|