common-address.js
5.35 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
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
/**
* 个人中心页-地址管理
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/02/24
*/
var $ = require('yoho-jquery');
var dialog = require('./dialog');
var Alert = dialog.Alert;
var address = {
// 省的控件ID
provinceDomId: 'province',
// 城市的控件ID
cityDomId: 'city',
// 地区的控件ID
areaDomId: 'areaCode',
initDomIds: function(domOptions) {
var the = this;
if (domOptions.provinceDomId) {
the.provinceDomId = domOptions.provinceDomId;
}
if (domOptions.cityDomId) {
the.cityDomId = domOptions.cityDomId;
}
if (domOptions.areaDomId) {
the.areaDomId = domOptions.areaDomId;
}
},
// 初始化地址数据
loadAreaData: function(pCode, toDomId, defaultValue, allCode) {
var the = this,
$toDom = $('#' + toDomId),
i = 0,
point = '';
var active,
val,
nId,
selecter;
$toDom.empty();
if (pCode < 91) {
$('#' + the.areaDomId).empty();
$('#county').attr('disabled', 'disabled');
}
$.ajax({
type: 'GET',
url: '/home/address/area',
data: 'id=' + pCode,
success: function(jsonData) {
jsonData.code = 200;
if (jsonData.code !== 200) {
active = new Alert('暂无数据');
active.show();
return false;
}
$toDom.append('<option value="0">' + defaultValue + '</option>');
for (i in jsonData.options) {
if (jsonData.options[i]) {
val = jsonData.options[i];
point = (toDomId === the.areaDomId && val.is_support === 'Y') ? '*' : '';
nId = val.value;
selecter = '';
if (typeof(allCode) !== 'undefined' && allCode !== 0 &&
nId === allCode.substr(0, nId.length)) {
selecter = 'selected';
}
$toDom.append('<option value="' + nId + '" ' + selecter + '>' + point + val.text + '</option>');
}
}
}
});
},
bindAreaChange: function(domOptions) {
var the = this;
if ($('#' + the.provinceDomId).data('events')) {
return;
}
// 初始化
$('#' + the.provinceDomId).change(function() {
var pCode = $('#' + the.provinceDomId).val();
if (pCode === '0') {
return;
}
the.loadAreaData(pCode, the.cityDomId, '请选择城市', pCode);
$('#' + the.areaDomId).hide();
the.showAreaSel(domOptions.dispDomId);
});
$('#' + this.cityDomId).change(function() {
var pCode = $('#' + the.cityDomId).val();
if (pCode === '0') {
return;
}
the.loadAreaData(pCode, the.areaDomId, '请选择区县', pCode);
$('#' + the.areaDomId).show();
the.showAreaSel(domOptions.dispDomId);
});
$('#' + the.areaDomId).change(function() {
the.showAreaSel(domOptions.dispDomId);
});
the.showAreaSel(domOptions.dispDomId);
},
/**
* 显示地区选择
*/
showAreaSel: function(dispDomId) {
var the = this,
strAddr = '',
strProvince = $('#' + the.provinceDomId).find('option:selected').text(),
strCity = $('#' + the.cityDomId).find('option:selected').text(),
strArea = $('#' + the.areaDomId).find('option:selected').text();
if (dispDomId && dispDomId !== '') {
if (strProvince.indexOf('选择') < 0) {
strAddr = strProvince;
}
if (strCity !== '' && strCity.indexOf('选择') < 0) {
strAddr += ',' + strCity;
}
if (strArea !== '' && strArea.indexOf('选择') < 0) {
strAddr += ',' + strArea;
}
$('#' + dispDomId).html(strAddr);
}
},
loadAllData: function(areaCode, domOptions) {
var the = this;
the.initDomIds(domOptions);
areaCode += '';
if (areaCode < 91) {
the.loadAreaData(0, the.provinceDomId, '请选择省份', '');
$('#' + the.areaDomId).hide();
$('#' + the.cityDomId).html('<option value="0">请选择市</option>');
} else if (areaCode.length === 4) {
the.loadAreaData(0, the.provinceDomId, '请选择省份', areaCode);
the.loadAreaData(areaCode.substr(0, 2), the.cityDomId, '请选择城市', areaCode);
the.loadAreaData(areaCode, the.areaDomId, '请选择区县', areaCode);
$('#' + the.areaDomId).show();
} else if (areaCode.length === 6) {
the.loadAreaData(0, the.provinceDomId, '请选择省份', areaCode);
the.loadAreaData(areaCode.substr(0, 2), the.cityDomId, '请选择城市', areaCode);
the.loadAreaData(areaCode.substr(0, 4), the.areaDomId, '请选择区县', areaCode);
$('#' + this.areaDomId).show();
}
the.bindAreaChange(domOptions);
}
};
module.exports = address;