Blogger Information
Blog 11
fans 0
comment 1
visits 15626
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ajax实现三级联动
JasonKim的博客
Original
922 people have browsed it
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>实现三级联动</title>
	<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
	省: <select id="pro"></select>
	市: <select id="city"></select>
	区/县: <select id="area"></select>


	<script type="text/javascript">
		$(function(){
			$.getJSON('1.json',function(data){
				let option = '<option value="">请选择(省)</option>';
				$.each(data,function(i) {
					option += '<option value="'+data[i].proId+'">'+data[i].proName+'</option>';
				})
				$('#pro').html(option);
			})

			// 改变事件
			$('#pro').change(function() {
				$('[value=""]').remove();
				let option = '<option value="">请选择(市)</option>';
				let proVal = parseInt($(this).val());
				$.getJSON('2.json',function(data) {
					$.each(data,function(i){
						if(data[i].proId == proVal) {
							option += '<option value="'+data[i].cityId+'">'+data[i].cityName+'</option>';
						}
					})
					$('#city').html(option);
				})
			})

			// 城市改变事件
			$('#city').change(function(){

				$('[value=""]').remove();
				let option = '<option value="">请选择(区/县)</option>';
				let cityVal = parseInt($(this).val());
				console.log(cityVal)
				$.getJSON('3.json',function(data) {
					$.each(data,function(i){
						if(data[i].cityId == cityVal) {
							option += '<option value="'+data[i].areaId+'">'+data[i].areaName+'</option>';
						}
					})
					$('#area').html(option);
				})
			})
		})
	</script>
</body>
</html>


Correction status:qualified

Teacher's comments:完成的不错,继续加油。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post