省市级联

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>province.html</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
 <script>
 var data =
  [
  {
   name:"湖北",
   id:99,
   cities:[
    {name:"武汉",id:3},
    {name:"宜昌",id:9},
    {name:"荆州",id:6}]
  },
  {
   name:"湖南",
   id:88,
   cities:[
    {name:"长沙",id:4},
    {name:"岳阳",id:7},
    {name:"郴州",id:10}]
  
  },
  {
   name:"四川",
   id:77,
   cities:[
    {name:"成都",id:5},
    {name:"宜宾",id:11},
    {name:"绵阳",id:8},    
    {name:"自贡",id:12}]  
  }
 ];

 
 function fillProvince()
 {   
   for(var i=0;i<data.length;i++)
  { 
   var provincedata = data[i];
   var optProvince = new Option(provincedata.name,provincedata.id); 
   document.getElementById("provinceId").options.add(optProvince); 
  } 
 }
  
 function clearOptions(selElement)
 {
  var options = selElement.options;
  var len = options.length;
  /*for(var i=0;i<len;i++)
  {
   options.remove(0);
  }*/
  for(var i=len-1;i>=0;i--)
  {
   selElement.remove(i);
  }  
 }
 
 function fillCity(provinceIndex)
 {
  //document.getElementById("cityId").options.length=0;
  clearOptions(document.getElementById("cityId")); 
  var citydata = data[provinceIndex].cities;
  for(var i=0; i<citydata.length; i++)
  { 

   var optCity = new Option(citydata[i].name,citydata[i].id); 
   document.getElementById("cityId").options.add(optCity);
  } 
 
 }
 </script>
  </head>
 
  <body>
    <select id="provinceId" name="provinceId" onChange="fillCity(this.selectedIndex)">
   
    </select>
    <select id="cityId" name="cityId">
   
    </select>   
 <script>
  fillProvince();
  fillCity(document.getElementById("provinceId").selectedIndex);
 </script>  
  </body>
</html>

 

 

===========================================

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>province.html</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
 <script>
 var data = [["湖北","99","武汉","3","宜昌","9","荆州","6"],
  ["湖南","88","长沙","4","岳阳","7","郴州","10"],
  ["四川","77","成都","5","宜宾","11","绵阳","8","自贡","12"]];
 
 function fillProvince()
 {   

  /*讲课时先要用演示
   document.getElementById("provinceId")
    .options.add(new Option("武汉","3"));
   这种静态数据测试,以便基础差的学员更容易理解
   */
   //todo here!!!!!
   

   for(var i=0; i<data.length; i++){
    var provincedata = data[i];
    document.getElementById("provinceId").options.add(new Option(provincedata[0],provincedata[1]));
   }
 }
  
 function clearOptions(selElement)
 {
  var options = selElement.options;
  var len = options.length;
  /*for(var i=0;i<len;i++)
  {
   options.remove(0);
  }*/
  for(var i=len-1;i>=0;i--)
  {
   //options.remove(i);在火狐下不行
   selElement.remove(i);
  }  
 }
 
 function fillCity(provinceIndex)
 {
  //document.getElementById("cityId").options.length=0;
  clearOptions(document.getElementById("cityId")); 
  
   //todo here!!!!! 

   
   var citydata = data[provinceIndex];
   
   for(var j=1; j<citydata.length/2; j++){
    document.getElementById("cityId").options.add(new Option(citydata[j*2],citydata[j*2+1]));
   }
  
  
 }
 </script>
  </head>
 
  <body>
    <select id="provinceId" name="provinceId" onChange="fillCity(this.selectedIndex)">
   
    </select>
    <select id="cityId" name="cityId">
   
    </select>   
 <script>
  fillProvince();
  fillCity(document.getElementById("provinceId").selectedIndex);
 </script>  
  </body>
</html>

 

====================================================

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值