-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.js
More file actions
77 lines (69 loc) · 1.82 KB
/
control.js
File metadata and controls
77 lines (69 loc) · 1.82 KB
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
var Linkage = {
provinces: document.getElementById('provinces'),
citys: document.getElementById('citys'),
datas: linkDatas,
isArray: function(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
},
addOptions: function(target, options){
var optionEle = null,
target = target,
options = options;
if(!this.isArray(options)){
return
}
var optionLen = options.length,
fragment = document.createDocumentFragment();
for(var i=0; i<optionLen; i++){
optionEle = document.createElement('option');
optionEle.value = options[i].value;
if(optionEle.innerText){
optionEle.innerText = options[i].text;
}else{
optionEle.textContent = options[i].text;
}
fragment.appendChild(optionEle);
}
target.appendChild(fragment);
},
provincesCitysLink: function(province, city){
var provinces = this.datas.provinces,
citys = this.datas.citys,
provincesWrap = [];
for(var i=0; i < provinces.length; i++){
provincesWrap.push({
'text': provinces[i].name,
'value': provinces[i].code
});
}
this.addOptions(province, provincesWrap);
this.addOptions(city, [{
'text': citys[0],
'value': citys[0]
}]);
},
init: function(){
var self = this;
if(self.provinces && self.citys){
self.provincesCitysLink(self.provinces, self.citys);
self.provinces.addEventListener('change', function(e){
/*this已经指向为select DOM对象*/
self.citys.innerHTML = '';
var selectedCity = self.datas.citys[this.value];
if(!self.isArray(selectedCity)){
return
}
var citysLen = selectedCity.length,
citysBox = [];
for(var i = 0; i < citysLen; i++){
citysBox.push({
'text': selectedCity[i],
'value': selectedCity[i]
});
}
self.addOptions(self.citys, citysBox);
})
}
}
}
Linkage.init();