web三级联动选择地址

地址三级联动,依赖城市city.js文件

<script type="text/javascript" src="jquery/jquery-2.2.4.min.js"></script>
<!-- 三级联动城市划分js -->
<script type="text/javascript" src="js/city.js"></script>

然后再script中初始化列表

<script type="text/javascript">	
	//三级联动
	//设置省份初始化
$(function(){
	
	var html="";
	$.each(city.citylist,function(index,item){
		html+="<option value='"+item.p+"'>"+item.p+"</option>";
	})
	
	$("select[name='province']").html(html);
	
	
	//
	html="";
	$.each(city.citylist,function(index,item){
		if(item.p=="北京"){
			$.each(item.c,function(index,c){
				html+="<option value='"+c.n+"'>"+c.n+"</option>";
			})
		}
	});
	$("select[name='prefecture']").html(html);
	
	
	//省份改变时候,市区做相应的改变
	$("select[name='province']").change(function(){
		var p=$("select[name='province']").val();
		//设置清空乡镇残余文字。
		$("select[name='county']").html("");
		
		html="";
		$.each(city.citylist,function(index,item){
			if(item.p==p){
				$.each(item.c,function(index,c){
					html+="<option value='"+c.n+"'>"+c.n+"</option>";
				})
			}
		});
		
		$("select[name='prefecture']").html(html);
	});
	
	$("select[name='prefecture']").change(function(){
		var p=$("select[name='province']").val();
		var c=$("select[name='prefecture']").val();
		
		html="";
		$.each(city.citylist,function(index,item){
			
			if(p==item.p){
				
				$.each(item.c,function(index,city){
					
					if(c==city.n){
						
						$.each(city.a,function(index,a){
							
							html+="<option value='"+a.s+"'>"+a.s+"</option>";
												
						})
					}
				});
			}
		});
		$("select[name='county']").html(html);
	})
});
	</script>

在body中显示标签

	<div>
				<label>城市:</label>
				<div>
					<select name="province" id="province"></select>
				</div>
			</div>
			<div>
				<div>
					<select name="prefecture" id="prefecture"></select>
				</div>
			</div>
			<div>
				<div>
					<select name="county" id="county"></select>
				</div>
			</div>

整个页面为:

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<base href="<%=basePath%>">
<title>用户注册</title>
<script type="text/javascript" src="jquery/jquery-2.2.4.min.js"></script>
<!-- 三级联动城市划分js -->
<script type="text/javascript" src="js/city.js"></script>
<script type="text/javascript">	
	//三级联动
	//设置省份初始化
$(function(){
	
	var html="";
	$.each(city.citylist,function(index,item){
		html+="<option value='"+item.p+"'>"+item.p+"</option>";
	})
	
	$("select[name='province']").html(html);
	
	
	//
	html="";
	$.each(city.citylist,function(index,item){
		if(item.p=="北京"){
			$.each(item.c,function(index,c){
				html+="<option value='"+c.n+"'>"+c.n+"</option>";
			})
		}
	});
	$("select[name='prefecture']").html(html);
	
	
	//省份改变时候,市区做相应的改变
	$("select[name='province']").change(function(){
		var p=$("select[name='province']").val();
		//设置清空乡镇残余文字。
		$("select[name='county']").html("");
		
		html="";
		$.each(city.citylist,function(index,item){
			if(item.p==p){
				$.each(item.c,function(index,c){
					html+="<option value='"+c.n+"'>"+c.n+"</option>";
				})
			}
		});
		
		$("select[name='prefecture']").html(html);
	});
	
	$("select[name='prefecture']").change(function(){
		var p=$("select[name='province']").val();
		var c=$("select[name='prefecture']").val();
		
		html="";
		$.each(city.citylist,function(index,item){
			
			if(p==item.p){
				
				$.each(item.c,function(index,city){
					
					if(c==city.n){
						
						$.each(city.a,function(index,a){
							
							html+="<option value='"+a.s+"'>"+a.s+"</option>";
												
						})
					}
				});
			}
		});
		$("select[name='county']").html(html);
	})
});
	</script>
</head>
<body>
			<div>
				<label>城市:</label>
				<div>
					<select name="province" id="province"></select>
				</div>
			</div>
			<div>
				<div>
					<select name="prefecture" id="prefecture"></select>
				</div>
			</div>
			<div>
				<div>
					<select name="county" id="county"></select>
				</div>
			</div>



</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值