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:" ...
随机推荐
- 如何高效的使用-Notepad++
Notepad++功能比 Windows 中的 Notepad(记事本)强大,除了可以用来制作一般的纯文字说明文件,也十分适合编写计算机程序代码.Notepad++ 不仅有语法高亮度显示,也有语法折叠 ...
- js中的json的小例子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- JavaScript 事件处理机制
DOM 事件流 DOM 是一个树型结构,当一个HTML元素产生一个事件时,该事件会在该元素结点与根节点之间按特定的顺序传播,路径所经过的节点都会收到该事件,这个传播过程可称为DOM事件流.而事件传播的 ...
- 数据库查询字段为null 时,返回0
oracle select nvl(字段名,0) from 表名; sqlserver select isnull(字段名,0) from 表名; mysql select ifnull(字段名,0) ...
- MyEclipse中搭建Struts2开发环境
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53205941 冷血之心的博客) 在MyEclipse中如何搭建St ...
- Fedora16的双显卡切换问题
症状:笔记本是Acer 4745G,安装了Fedora16+Win7 x64的双系统,每次开机后,独立显卡的风扇就开始狂转,同时笔记本的发热量极大,左侧出风口简直烫手.... 问题:Acer 4745 ...
- ubuntu更新下载源问题
Q1:ubuntu14.04系统安装完之后无法跟新并安装插件 cd /var/lib/apt/lists sudo rm * -rf sudo apt-get clean;sudo apt-get u ...
- nginx www解析失败问题解决
nginx www解析失败: nginx代理IIS下域名时 xxxx.xxx可以解析 但www.xxxx.xxx解析失败 IIS增加ip解析:配置下127.0.0.1就可以解析了.
- 基于localStorge开发登录模块的记住密码与自动登录
前沿||我是乐于分享,善于交流的鸟窝 先做写一篇关于登录模块中记住密码与自动登录的模块.鸟窝微信:jkxx123321 关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个 ...
- AWS CSAA -- 04 AWS Object Storage and CDN - S3 Glacier and CloudFront(二)
015 Version Control - Lab 016 Cross Region Replication 017 Lifecycle Management Glacier - Lab 018 C ...