好用的jquery操作select插件

  1. /* 
  2. 文件名:jquery.liu.select.js 
  3. 功能说明:本js文件为jquery类库的一个插件,主要实现对select的操作. 
  4. 作者:John Liu 
  5. 编写日期:2008/03/12 
  6. */  
  7. //得到select项的个数  
  8. jQuery.fn.size = function(){  
  9.     return jQuery(this).get(0).options.length;  
  10. }  
  11.   
  12. //获得选中项的索引  
  13. jQuery.fn.getSelectedIndex = function(){  
  14.     return jQuery(this).get(0).selectedIndex;  
  15. }  
  16.   
  17. //获得当前选中项的文本  
  18. jQuery.fn.getSelectedText = function(){  
  19.     if(this.size() == 0)  return "下拉框中无选项";  
  20.     else{  
  21.         var index = this.getSelectedIndex();        
  22.         return jQuery(this).get(0).options[index].text;  
  23.     }  
  24. }  
  25.   
  26. //获得当前选中项的值  
  27. jQuery.fn.getSelectedValue = function(){  
  28.     if(this.size() == 0)   
  29.         return "下拉框中无选中值";  
  30.       
  31.     else  
  32.         return jQuery(this).val();  
  33. }  
  34.   
  35. //设置select中值为value的项为选中  
  36. jQuery.fn.setSelectedValue = function(value){  
  37.     jQuery(this).get(0).value = value;  
  38. }  
  39.   
  40. //设置select中文本为text的第一项被选中  
  41. jQuery.fn.setSelectedText = function(text)  
  42. {  
  43.     var isExist = false;  
  44.     var count = this.size();  
  45.     for(var i=0;i<count;i++)  
  46.     {  
  47.         if(jQuery(this).get(0).options[i].text == text)  
  48.         {  
  49.             jQuery(this).get(0).options[i].selected = true;  
  50.             isExist = true;  
  51.             break;  
  52.         }  
  53.     }  
  54.     if(!isExist)  
  55.     {  
  56.         alert("下拉框中不存在该项");  
  57.     }  
  58. }  
  59. //设置选中指定索引项  
  60. jQuery.fn.setSelectedIndex = function(index)  
  61. {  
  62.     var count = this.size();      
  63.     if(index >= count || index < 0)  
  64.     {  
  65.         alert("选中项索引超出范围");  
  66.     }  
  67.     else  
  68.     {  
  69.         jQuery(this).get(0).selectedIndex = index;  
  70.     }  
  71. }  
  72. //判断select项中是否存在值为value的项  
  73. jQuery.fn.isExistItem = function(value)  
  74. {  
  75.     var isExist = false;  
  76.     var count = this.size();  
  77.     for(var i=0;i<count;i++)  
  78.     {  
  79.         if(jQuery(this).get(0).options[i].value == value)  
  80.         {  
  81.             isExist = true;  
  82.             break;  
  83.         }  
  84.     }  
  85.     return isExist;  
  86. }  
  87. //向select中添加一项,显示内容为text,值为value,如果该项值已存在,则提示  
  88. jQuery.fn.addOption = function(text,value)  
  89. {  
  90.     if(this.isExistItem(value))  
  91.     {  
  92.         alert("待添加项的值已存在");  
  93.     }  
  94.     else  
  95.     {  
  96.         jQuery(this).get(0).options.add(new Option(text,value));  
  97.     }  
  98. }  
  99. //删除select中值为value的项,如果该项不存在,则提示  
  100. jQuery.fn.removeItem = function(value)  
  101. {      
  102.     if(this.isExistItem(value))  
  103.     {  
  104.         var count = this.size();          
  105.         for(var i=0;i<count;i++)  
  106.         {  
  107.             if(jQuery(this).get(0).options[i].value == value)  
  108.             {  
  109.                 jQuery(this).get(0).remove(i);  
  110.                 break;  
  111.             }  
  112.         }          
  113.     }  
  114.     else  
  115.     {  
  116.         alert("待删除的项不存在!");  
  117.     }  
  118. }  
  119. //删除select中指定索引的项  
  120. jQuery.fn.removeIndex = function(index)  
  121. {  
  122.     var count = this.size();  
  123.     if(index >= count || index < 0)  
  124.     {  
  125.         alert("待删除项索引超出范围");  
  126.     }  
  127.     else  
  128.     {  
  129.         jQuery(this).get(0).remove(index);  
  130.     }  
  131. }  
  132. //删除select中选定的项  
  133. jQuery.fn.removeSelected = function()  
  134. {  
  135.     var index = this.getSelectedIndex();  
  136.     this.removeIndex(index);  
  137. }  
  138. //清除select中的所有项  
  139. jQuery.fn.clearAll = function()  
  140. {  
  141.     jQuery(this).get(0).options.length = 0;  
  142. }  

 

 

使用的时候先引入jquery.js文件,再引入jquery.liu.select.js文件,然后就可调用该插件的方法。比如,我要清除id为selEmail的下拉框中的所有项,那么我就可以这么操作:$("#selEmail").clearAll();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值