省级联动插件

组件的初始化:

初始化时可传入已有的数据进行展示。

var ele = {
				"province" : "山东",
				"city" : "临沂",
				"county" : "郯城县"
			}
			$("#linkage").provincialLinkage(ele);

js组件中的数据来源为本地的citys.json,网上可下载,或者联系我要。

js组件:

$.fn.extend({
	"provincialLinkage":function(ele){
		var _this = $(this);
		$.getJSON("js/citys.json",function(data){
			var _index = 0;
			var _indexcity = 0
			for (var i = 0; i < data.length; i++) {
				var _pro = '<option value="'+data[i].name+'">'+data[i].name+'</option>';
				_this.find(".province").append(_pro);
				
			}
			
			if(ele.province && ele.province != ""){
				$(".province option[value='"+ele.province+"']").attr("selected",true);
				var _p = _this.find(".province");
				_index = _p.find("option:selected").index();
				_this.find(".city option:gt(0)").remove();
				_this.find(".county option:gt(0)").remove();
				var _city = data[(_index-1)].child;
				for (var i = 0; i < _city.length; i++) {
					var _citys = '<option value="'+_city[i].name+'">'+_city[i].name+'</option>';
					_this.find(".city").append(_citys);
				}
			}
			/*获取选定的省份*/
			_this.find(".province").change(function(){
				_index = $(this).find("option:selected").index();
				_this.find(".city option:gt(0)").remove();
				_this.find(".county option:gt(0)").remove();
				var _city = data[(_index-1)].child;
				for (var i = 0; i < _city.length; i++) {
					var _citys = '<option value="'+_city[i].name+'">'+_city[i].name+'</option>';
					_this.find(".city").append(_citys);
				}
			})
			if (ele.city && ele.city != "") {
				$(".city option[value='"+ele.city+"']").attr("selected",true);
				var _c = _this.find(".city");
				_indexcity = _c .find("option:selected").index();
				_this.find(".county option:gt(0)").remove();
				var _county = data[(_index-1)].child[(_indexcity-1)].child;
				for (var i = 0; i < _county.length; i++) {
					var _countys = '<option value="'+_county[i].value+'">'+_county[i].value+'</option>';
					_this.find(".county").append(_countys);
				}

			}
			/*获取选定城市*/
			_this.find(".city").change(function(){
				_indexcity = $(this).find("option:selected").index();
				_this.find(".county option:gt(0)").remove();
				var _county = data[(_index-1)].child[(_indexcity-1)].child;
				for (var i = 0; i < _county.length; i++) {
					var _countys = '<option value="'+_county[i].value+'">'+_county[i].value+'</option>';
					_this.find(".county").append(_countys);
				}
			})
			
			if(ele.county && ele.county != ""){
				$(".county option[value='"+ele.county+"']").attr("selected",true);
			}
		});
	}
})

html部分:

<div id="linkage">
			<select name="province" class="province">
				<option value="" selected="selected">请选择</option>
			</select>
			<select name="city" class="city">
				<option value="" selected="selected">请选择</option>
			</select>
			<select name="county" class="county">
				<option value=""selected="selected">请选择</option>
			</select>
		</div>

css部分:

select{
				line-height: 28px;
			    height: 28px;
			    width: 130px;
			    border-radius: 2px;
			    border-color: #ddd;
			    padding: 0 10px;
			}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值