selectCity.js
6.36 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
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
/**
* 选择城市插件
* 使用:
* 1. 创建一个DIV做为容器, 然后设置变量
* var config = {
box: "#cityBox"
, names: {
province : "obj.enterprise.province"
, city: "obj.enterprise.city"
, county: "obj.enterprise.county"
, text: "obj.address"
}
,vals: [
151, 3001, 1510103
]
}
var sCity = $.selectCity(config);
*/
(function ($) {
var config = {
//存放列表的容器
box: ""
//各个下拉的名字
, names: {
//省
province : "province"
//市
, city: "city"
//区
, county: "county"
//文本
, text: ""
}
//默认值
,vals:[]
//请求地址
, url: location.protocol + "//" + location.hostname + "/regedit/cityInformation?parentId="
}
var html = '<select class="citySelect u-sel-addr" id="address_province" name="$province" sublevel="address_city"></select>'
+ '<select class="citySelect u-sel-addr" id="address_city" name="$city" sublevel="address_county"></select>'
+ '<select class="citySelect u-sel-addr" id="address_county" name="$county"></select>'
+ '<input type="hidden" name="$text" id="address_text" value=""/>';
var sCity = {
/**
* 初始化列表
* 根据父级ID号获取列表
* 同时根据列表设置默认值
*/
fillList: function (list, id, vals) {
var self = this;
var tempUrl = config.url;
if (id) {
tempUrl += id;
}
var def = "";
if(vals instanceof Array && vals && vals.length > 0){
def = vals.shift();
}
$.ajax({
url: tempUrl, dataType: "jsonp", success: function (data) {
self.cleanAddress(list);
for (var i = 0; i < data.length; i++) {
var d = data[i];
var selected = ""
if(def == d.regionId){
selected = "selected"
}
var option = $("<option value='" + d.regionId + "' "+selected+">" + d.regionName + "</option>");
list.append(option);
}
self.fillNext(list, def, vals);
self.refreshText();
}, error: function (e) {
}
});
}
/**
* 填充下一级
* @param obj
* @param val
* @param def
*/
,fillNext: function(obj, val, def){
var self = this;
var sub = self.fetchSub(obj);
if (sub) {
if (val == null || val == "") {
self.cleanAddress(sub);
return;
}
self.fillList(sub, val, def);
}
}
/**
* 清除下拉
* @param list
*/, cleanAddress: function (list) {
var self = this;
list.html("");
if(list.attr("id") == "address_province"){
list.append("<option value=''>请选择省</option>");
}else if(list.attr("id") == "address_city"){
list.append("<option value=''>请选择市</option>");
}else{
list.append("<option value=''>请选择区/县</option>");
}
var sub = self.fetchSub(list);
if (sub) {
self.cleanAddress(sub);
}
}
/**
* 清除所有
*/, cleanAllAddress: function () {
var self = this;
self.fillList($("#address_province"));
}
/**
* 下级下拉列表
* @param obj
* @returns {*|jQuery|HTMLElement}
*/, fetchSub: function (obj) {
var subLevel = $(obj).attr("sublevel");
if (subLevel) {
var sub = $("#" + subLevel);
return sub;
}
}
/**
* 插入HTML
*/
, insertHtml: function () {
var box = $(config.box);
var self = this;
var tag = html;
tag = tag.replace("$province", config.names.province);
tag = tag.replace("$city", config.names.city);
tag = tag.replace("$county", config.names.county);
tag = tag.replace("$text", config.names.text);
box.append(tag);
self.obj = {}
self.obj.province = $("#address_province");
self.obj.city = $("#address_city");
self.obj.county = $("#address_county");
self.obj.text = $("#address_text");
}
/**
* 绑定事件
*/, bind: function () {
var self = this;
$(".citySelect").each(function () {
$(this).change(function () {
var val = $(this).val();
var obj = $(this);
self.fillNext(obj, val);
self.refreshText();
});
});
}
/**
* 刷新文本
*/
, refreshText: function(){
var self = this;
var po = self.obj.province;
var co = self.obj.city;
var yo = self.obj.county;
var txt = self.obj.text;
var address = "";
if(po.val() && po.val != ""){
address += po.find("option:selected").text();
}
if(co.val() && co.val() != ""){
address += co.find("option:selected").text();
}
if(yo.val() && yo.val != ""){
address += yo.find("option:selected").text();
}
txt.val(address);
}
/**
* 初始化
*/
, init: function () {
this.insertHtml();
this.bind();
var dv = config.vals;
this.fillList($("#address_province"), "", dv);
}
};
$.selectCity = function (cfg) {
config = $.extend(config, cfg);
var t = sCity;
t.init();
return t;
};
})(jQuery);