forked from ciaoca/cxSelect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
331 lines (298 loc) · 10.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>cxSelect 联动下拉菜单 » 在线演示 - 前端开发仓库</title>
<style>
body{background:#ddd;font:14px/1.7 tahoma,'\5fae\8f6f\96c5\9ed1',sans-serif;}
h1,h2,h3{font-size:36px;line-height:1;}
h2{font-size:24px;}
h3{font-size:18px;}
fieldset{margin:2em 0;}
fieldset legend{font-weight:bold;font-size:16px;line-height:2;}
select,button{padding:0.5em;}
a{color:#06f;text-decoration:none;}
a:hover{color:#00f;}
.wrap{width:900px;margin:0 auto;padding:20px 50px;border-radius:8px;background:#fff;box-shadow:0 0 10px rgba(0,0,0,0.2);}
</style>
</head>
<body>
<div class="wrap">
<h1>cxSelect 联动下拉菜单</h1>
<h2>示例</h2>
<p>如果无法通过 AJAX 获取数据,请运行在服务器环境下。</p>
<fieldset id="city_china">
<legend>默认</legend>
<p>省份:<select class="province other">
<option>请选择</option>
</select></p>
<p>城市:<select class="city">
<option>请选择</option>
</select></p>
<p>地区:<select class="area">
<option>请选择</option>
</select></p>
</fieldset>
<fieldset id="city_china_val">
<legend>设置默认值及选项标题</legend>
<p>所在地区:
<select class="province other" data-first-title="选择省">
<option value="">请选择</option>
<option value="浙江省" selected>浙江省</option>
</select>
<select class="city" data-first-title="选择市">
<option value="">请选择</option>
<option value="杭州市" selected>杭州市</option>
</select>
<select class="area" data-first-title="选择地区">
<option value="">请选择</option>
<option value="西湖区" selected>西湖区</option>
</select>
</p>
</fieldset>
<!-- <fieldset id="city_china_val">
<legend>多种设置默认值的方式</legend>
<p>方法一:使用 option 的 value 和 selected 属性</p>
<p><select class="province">
<option>浙江省</option>
</select></p>
<p>如果有多个 option ,需要使用 selected 属性指定选中项</p>
<p><select class="city">
<option>请选择</option>
<option value="杭州市" selected>杭州市</option>
</select></p>
<p>方法二:使用 select 的 data-value 属性</p>
<p><select class="area" data-value="西湖区">
<option>请选择</option>
</select></p>
</fieldset> -->
<fieldset id="global_location">
<legend>全球主要国家城市联动</legend>
<p>所在地区:
<select class="country" data-first-title="选择国家"></select>
<select class="state" data-required="true"></select>
<select class="city" data-required="true"></select>
<select class="region" data-required="true"></select>
</p>
</fieldset>
<fieldset id="custom_data">
<legend>自定义选项</legend>
<p>一:<select class="first"></select></p>
<p>二:<select class="second"></select></p>
<p>三:<select class="third"></select></p>
<p>四:<select class="fourth"></select></p>
<p>五:<select class="fifth"></select></p>
</fieldset>
<fieldset>
<legend>API 接口</legend>
<div id="api_data">
<dl>
<dt>选择器组 A</dt>
<dd>
<select class="province select"></select>
<select class="city select"></select>
<select class="area select"></select>
</dd>
</dl>
<dl>
<dt>选择器组 B</dt>
<dd>
<select class="first select"></select>
<select class="second select"></select>
<select class="third select"></select>
<select class="fourth select"></select>
<select class="fifth select"></select>
</dd>
</dl>
</div>
<p>
<button type="button" class="button green" name="attach" value="">attach</button>
<button type="button" class="button green" name="detach" value="">detach</button>
<button type="button" class="button green" name="clear" value="">clear</button>
</p>
<p>
<button type="button" class="button green" name="setSelect" value="b">更换 select 组</button>
<button type="button" class="button green" name="setData" value="china">更换数据:国内数据</button>
<button type="button" class="button green" name="setData" value="global">更换数据:全球数据</button>
<button type="button" class="button green" name="setData" value="custom">更换数据:自定义数据</button>
</p>
<p>
<button type="button" class="button green" name="required" value="0">更改 required</button>
<button type="button" class="button green" name="emptyStyle" value="">更改 emptyStyle</button>
<button type="button" class="button green" name="firstTitle" value="请选择">更改 firstTitle</button>
</p>
</fieldset>
<h2>文档</h2>
<ul>
<li><a target="_blank" href="https://github.com/ciaoca/cxSelect">Github</a></li>
<li><a target="_blank" href="http://code.ciaoca.com/jquery/cxSelect/">中文文档</a></li>
</ul>
<h2>作者</h2>
<p><a target="_blank" href="http://ciaoca.com/">http://ciaoca.com/</a></p>
<p>Released under the MIT license</p>
</div>
<script src="http://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
<!-- <script src="http://cdn.staticfile.org/zepto/1.0/zepto.min.js"></script> -->
<script src="js/jquery.cxselect.js"></script>
<script>
(function() {
var urlChina = 'js/cityData.min.json';
var urlGlobal = 'js/globalData.min.json';
var dataCustom = [
{'v': '1', 'n': '第一级 >', 's': [
{'v': '2', 'n': '第二级 >', 's': [
{'v': '3', 'n': '第三级 >', 's': [
{'v': '4', 'n': '第四级 >', 's': [
{'v': '5', 'n': '第五级 >', 's': [
{'v': '6', 'n': '第六级 >'}
]}
]}
]}
]}
]},
{'v': 'test number', 'n': '测试数字', 's': [
{'v': 'text', 'n': '文本类型', 's': [
{'v': '4', 'n': '4'},
{'v': '5', 'n': '5'},
{'v': '6', 'n': '6'},
{'v': '7', 'n': '7'},
{'v': '8', 'n': '8'},
{'v': '9', 'n': '9'},
{'v': '10', 'n': '10'}
]},
{'v': 'number', 'n': '数值类型', 's': [
{'v': 11, 'n': 11},
{'v': 12, 'n': 12},
{'v': 13, 'n': 13},
{'v': 14, 'n': 14},
{'v': 15, 'n': 15},
{'v': 16, 'n': 16},
{'v': 17, 'n': 17}
]}
]},
{'v': 'test boolean','n': '测试 Boolean 类型', 's': [
{'v': true ,'n': true},
{'v': false ,'n': false}
]},
{v: 'test quotes', n: '测试属性不加引号', s: [
{v: 'quotes', n: '引号'}
]},
{v: 'test other', n: '测试奇怪的值', s: [
{v: '[]', n: '数组(空)'},
{v: [1,2,3], n: '数组(数值)'},
{v: ['a','b','c'], n: '数组(文字)'},
{v: new Date(), n: '日期'},
{v: new RegExp('\\d+'), n: '正则对象'},
{v: /\d+/, n: '正则直接量'},
{v: {}, n: '对象'},
{v: document.getElementById('custom_data'), n: 'DOM'},
{v: null, n: 'Null'},
{n: '未设置 value'}
]},
{'v': '' , 'n': '无子级'}
];
$.cxSelect.defaults.url = urlChina;
// 默认
$('#city_china').cxSelect({
selects: ['province', 'city', 'area']
});
// 设置默认值及选项标题
$('#city_china_val').cxSelect({
selects: ['province', 'city', 'area'],
emptyStyle: 'none'
});
// 全球主要国家城市联动
$('#global_location').cxSelect({
url: urlGlobal,
selects: ['country', 'state', 'city', 'region'],
emptyStyle: 'none'
});
// 自定义选项
$('#custom_data').cxSelect({
selects: ['first', 'second', 'third', 'fourth', 'fifth'],
// required: true,
jsonValue: 'v',
data: dataCustom
});
// API 接口
var apiBox = $('#api_data');
var cxSelectApi;
apiBox.cxSelect({
selects: ['province', 'city', 'area']
}, function(api) {
cxSelectApi = api;
});
// cxSelectApi = $.cxSelect(apiBox, {
// selects: ['province', 'city', 'area']
// });
$('body').on('click', 'button', function() {
var _name = this.name;
var _value = this.value;
switch (_name) {
case 'attach':
cxSelectApi.attach();
break;
case 'detach':
cxSelectApi.detach();
break;
case 'clear':
cxSelectApi.clear();
break;
case 'required':
cxSelectApi.setOptions({
required: _value == 1 ? false : true
});
this.value = _value == 1 ? 0 : 1;
break;
case 'emptyStyle':
if (_value === 'none') {
_value = 'hidden';
} else if (_value === 'hidden') {
_value = '';
} else {
_value = 'none';
};
cxSelectApi.setOptions({
emptyStyle: _value
});
this.value = _value;
break;
case 'firstTitle':
_value = _value === '请选择' ? '选择吧' : '请选择';
cxSelectApi.setOptions({
firstTitle: _value
});
this.value = _value;
break;
case 'setSelect':
cxSelectApi.setOptions({
selects: _value === 'a' ? ['province', 'city', 'area'] : ['first', 'second', 'third', 'fourth', 'fifth']
});
this.value = _value === 'a' ? 'b' : 'a';
break;
case 'setData':
if (_value === 'china' || _value === 'global') {
// $.ajax({
// url: this.value === 'china' ? urlChina : urlGlobal,
// type: 'GET',
// dataType: 'json'
// }).done(function(data, textStatus, jqXHR) {
cxSelectApi.setOptions({
url: this.value === 'china' ? urlChina : urlGlobal,
// data: data
});
// }).fail(function(jqXHR, textStatus, errorThrown) {
// });
} else if (this.value === 'custom') {
cxSelectApi.setOptions({
data: dataCustom
});
};
break;
// not default
};
});
})();
</script>
</body>
</html>