js实现二级三级联动

代码的解析请跳转到:https://blog.csdn.net/weixin_45925436/article/details/106364412

<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<table border="1">
<tr>
				<td>籍贯</td>
				<td><select  onchange="citys()" id="pronvice">
						<option value="-1" selected>请选择
					</select><select id="city" onchange="xians()">
						<option value="-1" selected>请选择
					</select><select id="xian" onclick="dj()">
						<option value="-1" selected>请选择
					</select>
				</td>
				<td><span id="span4"></span></td>

			</tr>
			</table>
			</body>
			<script type="text/javascript">
			//三级联动
	 var arr = [  [1,"河南",0],[2,"河北",0],[3,"陕西",0],
							   [11,"洛阳",1], [12,"偃师市",1],
							   [111,"白马寺",11], [112,"首阳山",12],
							   [21,"石家庄",2],[22,"邯郸",2],
							   [211,"大名",21],[212,"魏县",22],
							   [31,"西安",3],
							   [311,"长安",31],[311,"宝鸡",31],
				          ];
	 province();
					//获取省的数据  纵坐标是固定的
				    function province(){
						var str = "<option value='-1'>请选择</option>";
						for(var i=0;i<arr.length;i++){
							//alert(arr[i][2]);
							if(arr[i][2]==0){
								str+="<option value='"+arr[i][0]+"'>"+arr[i][1]+"</option>";
							}
						}
						//alert(str);
						//给省的下拉列表赋值
						document.getElementById("pronvice").innerHTML=str;
					}
					//加载市的数据
					function citys(){
						var pro = document.getElementById("pronvice").value;
						//alert(pro);
						var str = "<option value='-1'>请选择</option>";
						for(var i=0;i<arr.length;i++){
							if(arr[i][2]==pro){
								str+= "<option value='"+arr[i][0]+"'>"+arr[i][1]+"</option>";
							}
						}
						//alert(str);
						//给市的下拉列表赋值
						document.getElementById("city").innerHTML=str;
						
					}   
					function xians(){
						var pro = document.getElementById("city").value;
						//alert(pro);
						var str = "<option value='-1'>请选择</option>";
						for(var i=0;i<arr.length;i++){
							if(arr[i][2]==pro){
								str+= "<option value='"+arr[i][0]+"'>"+arr[i][1]+"</option>";
							}
						}
						//alert(str);
						//给市的下拉列表赋值
						document.getElementById("xian").innerHTML=str;
						
					}
			</script>
			</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值