1. 程式人生 > >jQuery完成省市二級聯動

jQuery完成省市二級聯動

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>使用jQuery完成省市二級聯動</title>
		<style type="text/css">
			.top{
				border: 1px solid red;
				width: 32.9%;
				height: 50px;
				float: left;
			}

			#clear{
				clear: both;
			}
			#menu{
				border: 1px solid blue;
				width: 99%;
				height: 40px;
				background-color: black;
			}
			#menu ul li{
				display: inline;
				color: white;
				font-size: 19px;
			}
			#bottom{
				text-align: center;
			}
			#contanier{
				border: 1px solid red;
				width: 99%;
				height: 600px;
				background: url(../img/regist_bg.jpg);
				position: relative;
			}
			#content{
				border: 5px solid gray;
				width: 50%;
				height: 60%;
				position: absolute;
				top: 100px;
				left: 300px;
				background-color: white;
				padding-top: 50px;
			}

		</style>
		<script type="text/javascript" src="../js/jquery-1.8.3.js" ></script>
		<script>
			$(function(){
				//2.建立二維陣列用於儲存省份和城市
				var cities = new Array(3);
				cities[0] = new Array("武漢市","黃岡市","襄陽市","荊州市");
				cities[1] = new Array("長沙市","郴州市","株洲市","岳陽市");
				cities[2] = new Array("石家莊市","邯鄲市","廊坊市","保定市");
				cities[3] = new Array("鄭州市","洛陽市","開封市","安陽市");
				
				$("#province").change(function(){
					//10.清除第二個下拉列表的內容
					$("#city").empty();
					
					//1.獲取使用者選擇的省份
					var val = this.value;
					//alert(val);
					//3.遍歷二維陣列中的省份
					$.each(cities,function(i,n){
						//alert(i+":"+n);
						//4.判斷使用者選擇的省份和遍歷的省份
						if(val==i){
							//5.遍歷該省份下的所有城市
							$.each(cities[i], function(j,m) {
								//alert(m);
								//6.建立城市文字節點
								var textNode = document.createTextNode(m);
								//7.建立option元素節點
								var opEle = document.createElement("option");
								//8.將城市文字節點新增到option元素節點中去
								$(opEle).append(textNode);
								//9.將option元素節點追加到第二個下拉列表中去
								$(opEle).appendTo($("#city"));
							});
						}
					});
					
				});
			});
		</script>
			
	</head>
	<body>
		<div>
			
			<!--1.logo部分的div-->
			<div>
				<!--切分為3個小的div-->
				<div class="top">
					<img src="../img/logo2.png" height="47px"/>
				</div>
				<div class="top">
					<img src="../img/header.png" height="47px"/>
				</div>
				<div class="top" style="padding-top: 15px;height: 35px;">
					<a href="#">登入</a>
					<a href="#">註冊</a>
					<a href="#">購物車</a>
				</div>
			</div>
			<!--清除浮動-->
			<div id="clear">
				
			</div>
			<!--2.導航欄部分的div-->
			<div id="menu">
				<ul>
					<li >首頁</li>
					<li >電腦辦公</li>
					<li >手機數碼</li>
					<li >孕嬰保健</li>
					<li >鞋靴箱包</li>
				</ul>
			</div>
			<!--3.中間登錄檔單部分div-->
			<div id="contanier">
				<div id="content">
					<table border="1" align="center" cellpadding="0" cellspacing="0" width="70%" height="70%" bgcolor="white">
						<form method="get" action="#" onsubmit="return checkForm()">
						<tr>
							<td colspan="2" align="center">
								<font size="5">會員註冊</font>
							</td>
							
						</tr>
						<tr>
							<td>
								使用者名稱
							</td>
							<td>
								<input type="text" name="username" id="username" onfocus="showTips('username','必須以字母開頭')" onblur="check('username','使用者名稱不能為空')" /><span id="usernamespan"></span>
							</td>
						</tr>
						<tr>
							<td>密碼</td>
							<td>
								<input type="password" name="password" id="password" onfocus="showTips('password','密碼長度不能低於6位!')" onblur="check('password','密碼不能為空!')" /><span id="passwordspan"></span>
							</td>
						</tr>
						<tr>
							<td>確認密碼</td>
							<td>
								<input type="password" name="repassword" />
							</td>
						</tr>
						<tr>
							<td>email</td>
							<td>
								<input type="text" name="email" id="email" />
							</td>
						</tr>
						<tr>
							<td>姓名</td>
							<td>
								<input type="text" name="name" />
							</td>
						</tr>
						<!--1.編寫HTML檔案部分的內容-->
						<tr>
							<td>籍貫</td>
							<td>
								<!--2.確定事件,通過函式傳參的方式拿到改變後的城市-->
								<select id="province">
									<option>--請選擇--</option>
									<option value="0">湖北</option>
									<option value="1">湖南</option>
									<option value="2">河北</option>
									<option value="3">河南</option>
								</select>
								<select id="city">
									
								</select>
							</td>
						</tr>
						<tr>
							<td>性別</td>
							<td>
								<input type="radio" name="sex" value="男"/>男
								<input type="radio" name="sex" value="女"/>女
							</td>
						</tr>
						<tr>
							<td>出生日期</td>
							<td>
								<input type="text" name="birthday" />
							</td>
						</tr>
						<tr>
							<td>驗證碼</td>
							<td>
								<input type="text" name="yanzhengma" />
								<img src="../img/yanzhengma.png" />
							</td>
						</tr>
						<tr>
							<td colspan="2">
								<input type="submit" value="註冊" />											
							</td>
						</tr>
						</form>
					</table>
				</div>
			</div>
			
		</div>
	</body>
</html>