以下是操作 下拉列表的常用功能:
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() == 0) return "下拉框中無(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 }