使用jQuery实现select级联效果

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>New Web Project</title>
		
		<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
		
		<script type="text/javascript">
			$(function(){
				var anhui_city = ["合肥", "芜湖", "蚌埠"];			
				var hebei_city = ["北京", "燕郊"];
				var shandong_city = ["济南", "青岛"];
				
				$("select[name='province']").change(function() {
					//被选中的option
					var selected_value = $(this).val();
					
					if(selected_value == "select"){
						$("select[name='city']").empty();
						
					}else if(selected_value == "anhui"){ //安徽
						$("select[name='city']").empty();
						//循环添加
						for(var i = 0; i < anhui_city.length; i++) {
							var option = $("<option>").val(anhui_city[i]).text(anhui_city[i]);
      						$("select[name='city']").append(option);
						}
						
					}else if(selected_value == "hebei"){ //河北
						$("select[name='city']").empty();
						//循环添加
						for(var i = 0; i < hebei_city.length; i++) {
							var option = $("<option>").val(hebei_city[i]).text(hebei_city[i]);
      						$("select[name='city']").append(option);
						}
						
					}else if(selected_value == "shandong"){ //山东
     					$("select[name='city']").empty();
						//循环添加
						for(var i = 0; i < shandong_city.length; i++) {
							var option = $("<option>").val(shandong_city[i]).text(shandong_city[i]);
      						$("select[name='city']").append(option);
						}
					}
				});
								
			});
			
		</script>
		
	</head>
	<body>
		
		<select id="province" name="province">
			<option value="select">请选择..</option>	
			<option value="anhui">安徽</option>	
			<option value="hebei">河北</option>	
			<option value="shandong">山东</option>			
		</select>
		
		<select name="city">
			
		</select>
		
	</body>
</html>

你可能感兴趣的:(使用jQuery实现select级联效果)