1 function Select(){};
2 /**
3 * 根据指定的JSON对象来生成指定的select的options项(清除原来的options).
4 */
5 Select.create = function(/*string*/selectId,/*json object*/json) {
6 Select.clear(selectId);
7 Select.add(selectId, json);
8 };
9 /**
10 * 该方法同create,只不过是在原来的基础上进行追加
11 */
12 Select.add = function(/*string*/selectId,/*json object*/json) {
13 try {
14 if (!json.options) return;
15 for (var i = 0; i < json.options.length; i ++) {
16 Select.addOption(selectId,json.options[i].value,json.options[i].text);
17 }
18 } catch (ex) {
19 alert('设置select错误:指定的JSON对象不符合Select对象的解析要求!');
20 }
21 };
22 /**
23 * 创建一个options并返回
24 */
25 Select.createOption = function(/*string*/value, /*string*/text) {
26 var opt = document.createElement('option');
27 opt.setAttribute('value', value);
28 opt.innerHTML = text;
29 return opt;
30 };
31 /**
32 * 给指定的select添加一个option,并返回当前option对象
33 */
34 Select.addOption = function(/*string*/selectId, /*string*/value, /*string*/text) {
35 var opt = Select.createOption(value, text);
36 $(selectId).appendChild(opt);
37 return opt;
38 };
39 /**
40 * 获取指定select的当前被选中的options对象,如果为多选且有多个被选中则返回数组.
41 */
42 Select.getSelected = function(/*string*/selectId) {
43 var slt = $(selectId);
44 if (!slt) return null;
45 if (slt.type.toLowerCase() == "select-multiple") {
46 var len = Select.len(selectId);
47 var result = [];
48 for (var i = 0; i < len; i ++) {
49 if (slt.options[i].selected) result.push(slt.options[i]);
50 }
51 return result.length > 1 ? result : (result.length == 0 ? null : result[0]);
52 } else {
53 var index = $(selectId).selectedIndex;
54 return $(selectId).options[index];
55 }
56 };
57 /**
58 * 使指定索引位置的option被选中.从0开始.
59 */
60 Select.select = function(/*string*/selectId, /*int*/index) {
61 var slt = $(selectId);
62 if (!slt) return false;
63 for (var i = 0; i < slt.options.length; i ++) {
64 if (index == i) {
65 slt.options[i].setAttribute("selected", "selected");
66 return true;
67 }
68 }
69 return false;
70 };
71 /**
72 * 选中指定的select的所有option选项,如果支持多选的话
73 */
74 Select.selectAll = function(/*string*/selectId) {
75 var len = Select.len(selectId);
76 for (var i = 0; i < len; i ++) Select.select(selectId, i);
77 };
78 /**
79 * 获取指定select的总的options个数
80 */
81 Select.len = function(/*string*/selectId) {
82 return $(selectId).options.length;
83 };
84 /**
85 * 清除select中满足条件的options,如果没有指定处理方法则清除所有options项
86 */
87 Select.clear = function(/*string*/selectId, /*function*/iterator) {
88 if (typeof(iterator) != 'function') {
89 $(selectId).length = 0;
90 } else {
91 var slt = $(selectId);
92 for (var i = slt.options.length - 1; i >= 0; i --) {
93 if (iterator(slt.options[i]) == true) slt.removeChild(slt.options[i]);
94 }
95 }
96 };
97 /**
98 * 复制指定的select的option对象到另外一指定的select对象上.如果指定了处理
99 * 函数,那么只有返回true时才会copy.
100 * 函数iterator参数:当前处理的option对象、目标select的options数组
101 */
102 Select.copy = function(/*string*/srcSlt, /*string*/targetSlt, /*function*/iterator) {
103 var s = $(srcSlt), t = $(targetSlt);
104 for (var i = 0; i < s.options.length; i ++) {
105 if (typeof(iterator) == 'function') {
106 if (iterator(s.options[i], $(targetSlt).options) == true) {
107 t.appendChild(s.options[i].cloneNode(true));
108 }
109 } else {
110 t.appendChild(s.options[i].cloneNode(true));
111 }
112 }
113 };