`
495081611
  • 浏览: 33876 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

jquery select

阅读更多
最近使用JQuery来编写前端js脚本,涉及到依据下拉框的值确定其他下拉框中数据项。需要实现对下拉框中候选项的添加删除等,使用js写的话简直会死人的。还好使用JQuery。以下是操作 下拉列表的常用功能:

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()  ==   0 )  return   " 下拉框中无选项 " ;  
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  }



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/SuperBoo/archive/2009/11/20/4839761.aspx
分享到:
评论

相关推荐

    jquery select2组件

    **jQuery Select2组件详解** `jQuery Select2`是一款强大的、高度可定制的下拉选择框插件,它在原生HTML `&lt;select&gt;` 元素的基础上提供了丰富的功能和优秀的用户体验。这款组件广泛应用于网页开发中,使得传统的单选...

    jquery select2

    为了解决这些问题,jQuery社区推出了一个强大的插件——jQuery Select2。Select2不仅提供了美观的界面,还增强了用户体验,特别是对于大型数据集的搜索功能,使得在下拉框中快速找到所需选项变得轻而易举。 一、...

    jquery select 多种下拉日期选择插件

    本文将深入探讨“jQuery select 多种下拉日期选择插件”的相关知识点,帮助开发者理解和应用此类插件。 首先,我们来解析标题:“jQuery select 多种下拉日期选择插件”。这个标题暗示了我们正在讨论一个基于jQuery...

    jQuery Select下拉框美化特效.zip

    "jQuery Select下拉框美化特效"就是为了提升用户体验,将原本朴素的Select元素转化为具有视觉吸引力的组件。 该特效主要基于JavaScript库jQuery实现,jQuery是一个广泛使用的JavaScript库,它简化了DOM操作、事件...

    jQuery Select(单选) 模拟插件 V1.3.6

    **jQuery Select(单选) 模拟插件 V1.3.6 知识点详解** 在网页开发中,我们经常需要使用到选择器来获取用户的选择,原生的HTML `&lt;select&gt;` 元素虽然简单易用,但在样式定制和交互效果上往往显得力不从心。为了改善这...

    jquery select2 select美化插件

    **jQuery Select2 美化插件详解** `jQuery Select2` 是一款广泛使用的JavaScript库,专门用于美化和增强HTML `&lt;select&gt;` 元素。它提供了丰富的功能和自定义选项,使得原本简单的下拉选择框变得功能强大且视觉上更具...

Global site tag (gtag.js) - Google Analytics