JS省市区联动效果
省市区联动下拉效果在WEB中应用非常广泛,尤其在电商网站最为常见。一般使用Ajax实现无刷新下拉联动。利用jQuery,通过读取JSON数据,实现无刷新动态下拉省市二(三)级联动效果。
首先我们可以看看自定义参数配置项如下:
参数配置如下:
| url | 'js/city.min.js', 省市区JSON数据 |
| provId | '#prov', 默认省份ID |
| cityId | '#city' 默认城市ID |
| areaId | '#area' 默认区ID |
| prov | null , 参数是否传入默认的省份 |
| city | null, 参数是否传入默认的市 |
| area | null , 参数是否传入默认的区 |
| required | true, 必填项 默认为true |
接下来HTML代码如下:
<div id="city_4">
<select class="prov" id="prov4"></select>
<select class="city" disabled="disabled" id="city4"></select>
<select class="dist" disabled="disabled" id="area4"></select>
</div>
如果没有三级联动的话 那么区的HTML代码可以省略掉!
调用非常简单如下:
new CitySelect({
provId : "#prov4",
cityId : '#city4',
areaId : '#area4',
prov:"湖南",
city:"长沙"
});
废话也不多说,贴代码如下:
HTML代码:
<div id="main">
<div class="demo">
<h3>直接调用</h3>
<p>二级联动,默认选项为:请选择</p>
<div id="city_1">
<select class="prov" id="prov1"></select>
<select class="city" disabled="disabled" id="city1"></select>
</div>
<p>三级联动,默认省份:北京,隐藏无数据的子级select</p>
<div id="city_2">
<select class="prov" id="prov2"></select>
<select class="city" disabled="disabled" id="city2"></select>
<select class="dist" disabled="disabled" id="area2"></select>
</div>
</div> <div class="demo">
<h3>设置省份、城市、地区(县)的默认值</h3>
<p>二级联动</p>
<div id="city_3">
<select class="prov" id="prov3"></select>
<select class="city" disabled="disabled" id="city3"></select>
</div>
<p>三级联动</p>
<div id="city_4">
<select class="prov" id="prov4"></select>
<select class="city" disabled="disabled" id="city4"></select>
<select class="dist" disabled="disabled" id="area4"></select>
</div>
</div> <div class="demo">
<h3>自定义下拉选项</h3>
<div id="city_5">
<select class="prov" id="prov5"></select>
<select class="city" disabled="disabled" id="city5"></select>
<select class="dist" disabled="disabled" id="area5"></select>
</div>
</div>
</div>
JS代码如下:
/**
* JS省市区联动
* @constructor CitySelect
* @author tugenhua
* @time 2014-2-22
* @email 879083421@qq.com
*/ function CitySelect(options) { this.config = {
url : "js/city.min.js",
provId : '#prov',
cityId : '#city',
areaId : '#area',
prov : null,
city : null,
area : null,
required : true
}; this.cache = {
select_prehtml : '', // 下拉框默认选项
city_json : '' // 城市json
}; this.init(options);
} CitySelect.prototype = { constructor : CitySelect, init: function(options) {
this.config = $.extend(this.config, options || {});
var self = this,
_config = self.config,
_cache = self.cache; _cache.select_prehtml = _config.required ? '' : "<option value=''>请选择</option>"; // 设置省市的数据
if(typeof(_config.url) == 'string') {
$.getJSON(_config.url, function(json) {
_cache.city_json = json;
self._provFunc();
});
}else {
_cache.city_json = _config.url;
self._provFunc();
}
},
/*
* 渲染省份函数
* @method _provFunc
*/
_provFunc: function() {
var self = this,
_config = self.config,
_cache = self.cache; var html = _cache.select_prehtml; // 遍历 获取省份
$(_cache.city_json.citylist).each(function(i,prov){
html += "<option value='"+prov.p+"'>"+prov.p+"</option>";
});
$(_config.provId).html(html); /*
* 若有传入省份与市级的值,则选中。(setTimeout为兼容IE6而设置)
* 发现取到的selectedIndex老是前面一次
*/
t && clearTimeout(t);
var t = setTimeout(function(){
if(_config.prov != null) {
$(_config.provId).val(_config.prov);
self._cityStart();
setTimeout(function(){
if(_config.city != null) {
$(_config.cityId).val(_config.city);
self._areaStart();
setTimeout(function(){
if(_config.area != null) {
$(_config.areaId).val(_config.area);
}
},1);
}
},1);
}
},1); // 选择省份时发生事件
$(_config.provId).unbind('change').bind('change',function(){
self._cityStart();
});
// 选择市级时发生事件
$(_config.cityId).unbind('change').bind('change',function(){
self._areaStart();
});
},
/*
* 渲染市函数
* @method _cityStart
*/
_cityStart: function() {
var self = this,
_config = self.config,
_cache = self.cache;
var prov_id = $(_config.provId).get(0).selectedIndex;
if(!_config.required){
prov_id--;
};
$(_config.cityId).empty().attr("disabled",true);
$(_config.areaId).empty().attr("disabled",true); if(prov_id < 0 || typeof(_cache.city_json.citylist[prov_id].c)=="undefined"){ return;
} var html = _cache.select_prehtml; $.each(_cache.city_json.citylist[prov_id].c,function(i,city){
html += "<option value='"+city.n+"'>"+city.n+"</option>";
}); $(_config.cityId).html(html).attr('disabled',false); self._areaStart();
},
/*
* 渲染区函数
* @method _areaStart
*/
_areaStart: function(){
var self = this,
_config = self.config,
_cache = self.cache;
var prov_id=$(_config.provId).get(0).selectedIndex,
city_id=$(_config.cityId).get(0).selectedIndex;
if(!_config.required){
prov_id--;
city_id--;
};
$(_config.areaId).empty().attr("disabled",true); if(prov_id<0||city_id<0||typeof(_cache.city_json.citylist[prov_id].c[city_id].a)=="undefined"){
return;
};
var html = _cache.select_prehtml; $.each(_cache.city_json.citylist[prov_id].c[city_id].a,function(i,area){
html += "<option value='"+area.s+"'>"+area.s+"</option>";
}); $(_config.areaId).html(html).attr('disabled',false); }
};
调用如下:
$(function(){
new CitySelect({
prov:'北京',
provId : "#prov1",
cityId : '#city1'
});
new CitySelect({
provId : "#prov2",
cityId : '#city2',
areaId : '#area2'
});
new CitySelect({
provId : "#prov3",
cityId : '#city3'
});
new CitySelect({
provId : "#prov4",
cityId : '#city4',
areaId : '#area4',
prov:"湖南",
city:"长沙"
});
new CitySelect({
provId : "#prov5",
cityId : '#city5',
areaId : '#area5'
});
});
JS省市区联动效果的更多相关文章
- JS省市区联动
JS省市区使用文档 一:服务器返回JSON格式要求如下网址里面data的格式:(拿KISSY组件data格式来做的) http://gallery.kissyui.com/cityselector/d ...
- js三级联动效果city-picker
链接:https://pan.baidu.com/s/1NE_EO5_xGvR-y-lboYap7g 提取码:h00e 效果展示: 解决: 动态赋值: 注意:在执行赋值之前,必须执行reset和des ...
- JS省市区三级联动
不需要访问后台服务器端,不使用Ajax,无刷新,纯JS实现的省市区三级联动. 当省市区数据变动是只需调正js即可. 使用方法: <!DOCTYPE html><html>< ...
- js之省市区(县)三级联动效果
省市区(县)三级联动效果,是我们软件开发比较常用的,特别是对一些crm,erp之类,当然也包括其他的后台管理系统,基本都涉及到,今天贴出这个常用的,方便个人复用和大家使用 <!DOCTYPE h ...
- QQ JS省市区三级联动
如下图: 首先写一个静态的页面: <!DOCTYPE html> <html> <head> <title>QQ JS省市区三级联动</title ...
- jquery.cityselect.js基于jQuery+JSON的省市或自定义联动效果
一.插件介绍 最早做省市联动的时候都特别麻烦,后来在helloweba的一篇文章中看到这个插件,很不错的,后来就一直用了. 省市区联动下拉效果在WEB中应用非常广泛,尤其在一些会员信息系统.电商网站最 ...
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- 从QQ网站中提取的纯JS省市区三级联动
在 http://ip.qq.com/ 的网站中有QQ自己的JS省市区三级联动 QQ是使用引用外部JS来实现三级联动的.JS如下:http://ip.qq.com/js/geo.js <!DOC ...
- 省市区联动JS脚本
省市区联动JS脚本 /* ***说明:省市区联动JS脚本 ***作者:Jerry Yuan */ var province=[{id:0,name:'选择省'},{id:11,name:" ...
随机推荐
- webhttpbinding、basichttpbinding和wshttpbinding的区别
webhttpbinding是REST风格的绑定,您只需点击一个URL,然后从Web服务中获取大量XML或JSON. basichttpbinding和wshttpbinding是两个基于SOAP的绑 ...
- SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...
- Docker在windows7上的安装
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...
- 【代码笔记】iOS-增加右侧按钮
一,工程图. 二,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup ...
- 正则匹配身份证有bug你知道么?
在开发中,我们需要验证用户的输入信息,多半采用正则验证,下面就是身份证证号的几种常用的正则表达式: var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x) ...
- tilestache + mbutil应用
1. 安装pip. 我们同样需要在Python的官网上去下载,下载地址是: https://pypi.python.org/pypi/pip#downloads 2. 解压. 解压pip-9.0.1. ...
- CSS盒模型详解(图文教程)
本文最初发表于博客园,并在GitHub上持续更新.以下是正文. 盒子模型 前言 盒子模型,英文即box model.无论是div.span.还是a都是盒子. 但是,图片.表单元素一律看作是文本,它们并 ...
- 03-01_WebLogic一些概念名词
WebLogic一些概念名词 域(Domain) 管理服务器(Administrative Server) 被管服务器(Managed Server,受管服务器) 集群(Cluster) 机器(Mac ...
- SqlServer PIVOT函数快速实现行转列,UNPIVOT实现列转行
我们在写Sql语句的时候没经常会遇到将查询结果行转列,列转行的需求,拼接sql字符串,然后使用sp_executesql执行sql字符串是比较常规的一种做法.但是这样做实现起来非常复杂,而在SqlSe ...
- HTML5学习资料
HTML5 的一些经典demo收集 Animated Books with CSS 3D Transforms 这是一个3D书本,CSS3完成 =========HTML5特效聚集网站======== ...