JQuery操作Selected

原文链接:http://dev.mjxy.cn/a-JQuery-Operation-Selected.aspx

/* 
002 文件名:jquery.liu.select.js 
003 功能说明:本js文件为jquery类库的一个插件,主要实现对select的操作. 
004 作者:John Liu 
005 编写日期:2008/03/12 
006 */  
007 //得到select项的个数  
008 jQuery.fn.size = function(){   
009     return jQuery(this).get(0).options.length;   
010 }   
011     
012 //获得选中项的索引  
013 jQuery.fn.getSelectedIndex = function(){   
014     return jQuery(this).get(0).selectedIndex;   
015 }   
016     
017 //获得当前选中项的文本  
018 jQuery.fn.getSelectedText = function(){   
019     if(this.size() == 0)  return "下拉框中无选项";   
020     else{   
021         var index = this.getSelectedIndex();         
022         return jQuery(this).get(0).options[index].text;   
023     }   
024 }   
025     
026 //获得当前选中项的值  
027 jQuery.fn.getSelectedValue = function(){   
028     if(this.size() == 0)    
029         return "下拉框中无选中值";   
030          
031     else  
032         return jQuery(this).val();   
033 }   
034     
035 //设置select中值为value的项为选中  
036 jQuery.fn.setSelectedValue = function(value){   
037     jQuery(this).get(0).value = value;   
038 }   
039     
040 //设置select中文本为text的第一项被选中  
041 jQuery.fn.setSelectedText = function(text)   
042 {   
043     var isExist = false;   
044     var count = this.size();   
045     for(var i=0;i<count;i++)   
046     {   
047         if(jQuery(this).get(0).options[i].text == text)   
048         {   
049             jQuery(this).get(0).options[i].selected = true;   
050             isExist = true;   
051             break;   
052         }   
053     }   
054     if(!isExist)   
055     {   
056         alert("下拉框中不存在该项");   
057     }   
058 }   
059 //设置选中指定索引项  
060 jQuery.fn.setSelectedIndex = function(index)   
061 {   
062     var count = this.size();       
063     if(index >= count || index < 0)   
064     {   
065         alert("选中项索引超出范围");   
066     }   
067     else  
068     {   
069         jQuery(this).get(0).selectedIndex = index;   
070     }   
071 }   
072 //判断select项中是否存在值为value的项  
073 jQuery.fn.isExistItem = function(value)   
074 {   
075     var isExist = false;   
076     var count = this.size();   
077     for(var i=0;i<count;i++)   
078     {   
079         if(jQuery(this).get(0).options[i].value == value)   
080         {   
081             isExist = true;   
082             break;   
083         }   
084     }   
085     return isExist;   
086 }   
087 //向select中添加一项,显示内容为text,值为value,如果该项值已存在,则提示  
088 jQuery.fn.addOption = function(text,value)   
089 {   
090     if(this.isExistItem(value))   
091     {   
092         alert("待添加项的值已存在");   
093     }   
094     else  
095     {   
096         jQuery(this).get(0).options.add(new Option(text,value));   
097     }   
098 }   
099 //删除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

       下载文件

posted @ 2011-07-08 17:16  敏捷学院  阅读(639)  评论(0编辑  收藏  举报