-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.hplinkage.js
executable file
·149 lines (148 loc) · 4.34 KB
/
jquery.hplinkage.js
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
/*
Ajax 三级省市联动
日期:2012-7-18
settings 参数说明
-----
url:省市数据josn文件路径
prov:默认省份
city:默认城市
dist:默认地区(县)
required:必选项
------------------------------ */
$.extend($.fn,{
hpLinkage: function( settings ){
settings = $.extend( true , {
url:"js/city.min.js",
prov: { selector: ".prov" , defaultvalue: "" },
city: { selector: ".city" , defaultvalue: "" },
dist: { selector: ".dist" , defaultvalue: "" },
required: true
} , settings ) ;
this.each(function( index , val ){
new hpLinkage( this , settings ) ;
});
return this;
}
});
function hpLinkage( context , settings ){
this.context = context ;
this.settings = settings ;
this.temple = settings.required ? "" : "<option value=''>请选择...</option>" ;
this.selected = { prov: "" , city: "" , dist: "" };
this.init = function(){
this.getAreaJson();
};
this.init();
};
hpLinkage.prototype.getAreaJson = function(){
var that = this ,
$this = $( this.context ) ,
settings = this.settings ;
if( Object.prototype.toString.call( settings.url ) == "[object String]" ){
$.ajax({
type: "get" ,
url: settings.url ,
dataType: "json" ,
success: function( data ){
that.AREAJSON = data;
that.onLinkage();
},
error: function( ){
that.AREAJSON = null;
NSLog( arguments ) ;
}
});
};
};
hpLinkage.prototype.onLinkage = function(){
if( !!!this.AREAJSON ) { return false; } // 不存在地区数据的情况
var that = this ,
$this = $( this.context ) ,
settings = this.settings ;
$this.on("change.hpLink", settings.prov.selector , function(){
var $this = $( this ) ,
value = $.trim( $this.val() ) ;
that.selected.prov = value ;
that.parseCityHTML() ;
}).on("change.hpLink", settings.city.selector , function(){
var $this = $( this ) ,
value = $.trim( $this.val() ) ;
that.selected.city = value ;
that.parseDistHTML( value ) ;
}).on("change.hpLink", settings.dist.selector , function(){
var $this = $( this ) ,
value = $.trim( $this.val() ) ;
that.selected.dist = value ;
});
that.parseProvHTML();
// 设置默认值
if( settings.prov.defaultvalue ){
$this.find( settings.prov.selector ).val( settings.prov.defaultvalue ).trigger("change.hpLink");
};
if( settings.city.defaultvalue ){
$this.find( settings.city.selector ).val( settings.city.defaultvalue ).trigger("change.hpLink");
};
if( settings.dist.defaultvalue ){
$this.find( settings.dist.selector ).val( settings.dist.defaultvalue );
};
};
hpLinkage.prototype.parseProvHTML = function(){
var that = this ,
$this = $( this.context ) ,
settings = this.settings ;
var provhtml = that.temple ;
$.each( that.AREAJSON , function( index , prov ){
provhtml += "<option value='" + prov.area_no + "'>" + prov.area_name + "</option>";
} );
$this.find( settings.prov.selector ).html( provhtml );
};
hpLinkage.prototype.parseCityHTML = function(){
var that = this ,
$this = $( this.context ) ,
settings = this.settings ,
prono = that.selected.prov ;
var cityhtml = that.temple ;
$.each( that.AREAJSON , function( index , prov ){
if( prov.area_no == prono ){
$.each( prov.subAreas , function( index , city ){
cityhtml += "<option value='" + city.area_no + "'>" + city.area_name + "</option>";
} );
return false;
}else{
return true;
};
} );
$this.find( settings.city.selector ).html( cityhtml );
$this.find( settings.dist.selector ).html( that.temple );
};
hpLinkage.prototype.parseDistHTML = function( prono ){
var that = this ,
$this = $( this.context ) ,
settings = this.settings ,
prono = that.selected.prov ,
cityno = that.selected.city ;
var disthtml = that.temple ;
$.each( that.AREAJSON , function( index , prov ){
if( prov.area_no == prono ){
$.each( prov.subAreas , function( index , city ){
if( city.area_no == cityno ){
$.each( city.subAreas , function( index , dist ){
disthtml += "<option value='" + dist.area_no + "'>" + dist.area_name + "</option>";
} );
return false;
}else{
return true;
};
} );
return false;
}else{
return true;
};
} );
$this.find( settings.dist.selector ).html( disthtml );
};
function NSLog( msg ){
if( window.console && window.console.log ){
console.log( msg , "未找到地址数据文件" );
};
};