首页 > 代码库 > 省市级联

省市级联

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
 var citys = new Array();
 citys["广东"] = ["汕头","揭阳","潮州"];
 citys["湖南"] = ["长沙","益阳","张家界"];
 
 function showCitys(){
  //清除市区下拉列表的选项
  document.getElementById("city").length=0;
  //获取省份下拉列表选中的值
  var pro = document.getElementById("province").value;
  //将对应的市区添加到市区下拉列表
  for(var city in citys[pro]){
   var c = new Option(citys[pro][city],citys[pro][city]); 
   document.getElementById("city").options.add(c);
  }
 }
</script>

</head>

<body>

省:<select id="province" onchange="showCitys();">
  <option value="">请选择省份</option>
        <option value="http://www.mamicode.com/广东">广东</option>
        <option value="http://www.mamicode.com/湖南">湖南</option>
 </select>
    <br />
市:<select id="city">
  
 </select>
</body>
</html>