jquery之动态级联下拉列表


jquery之动态级联下拉列表

在制作HTML页面的时候,难免会遇到级联下拉列表的显示。比如最常见的 “省---市” 级联下拉列表。

 

下面贴出来我的实现方式,供大家参考一下(用的是jquery)。

 

首先是运行结果:

 




 下面是代码,可以详细看看。

 

 

Html代码 复制代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.   <title>无标题文档</title>  
  6.   
  7.   <script src="./jquery-2.1.0.min.js"></script>  
  8.   
  9. <script>  
  10. $.fn.extend({  
  11.   parent_select_change: function(province) {  
  12.     $("#child_select").empty();  
  13.   
  14.     var parent = $("#parent_select").val();  
  15.   
  16.     if (parent == ''){  
  17.       return false;  
  18.     }  
  19.     var cities = null;  
  20.     for(var i=0; i < province.length; i++) {  
  21.       var p = province[i];  
  22.       if(p.id == parent){  
  23.         cities = p.city;  
  24.         break;  
  25.       }  
  26.     }  
  27.       
  28.     for(var i=0; i < cities.length; i++) {  
  29.       city = cities[i];  
  30.       var o = new Option(city.name,city.cid);  
  31.       $("#child_select").append(o);  
  32.     }  
  33.   },  
  34.   
  35.   show_province_items: function() {  
  36.     var resp = [  
  37.       {  
  38.         "id" : 1,  
  39.         "name" : "山西",  
  40.         "city" : [{  
  41.           "cid" : 1,  
  42.           "name" : "太原",  
  43.         }, {  
  44.           "cid" : 2,  
  45.           "name" : "临汾",  
  46.         }  
  47.         ],  
  48.       },  
  49.       {  
  50.         "id" : 2,  
  51.         "name" : "陕西",  
  52.         "city" : [{  
  53.           "cid" : 3,  
  54.           "name" : "榆林",  
  55.         }, {  
  56.           "cid" : 4,  
  57.           "name" : "西安",  
  58.         }  
  59.         ],  
  60.       },  
  61.     ];  
  62.       
  63.     //以上为模拟数据,可以用getJSON的方式,从服务器端取回来数据  
  64.     //$.getJSON("/province", function(resp){  
  65.         var html = ""  
  66.         var pro = resp  
  67.         html += "<option value=''> --请选择-- </option>"  
  68.         if(pro){  
  69.           for(var i=0; i < pro.length; i++) {  
  70.             var p = pro[i];  
  71.             html += "<option value='" + p.id + "'>" + p.name + "</option>"  
  72.           }  
  73.         }  
  74.           
  75.         $("#parent_select").empty();  
  76.         $("#parent_select").html(function(i,origText){  
  77.           return html;  
  78.         });  
  79.   
  80.         $("#parent_select").change(function(){  
  81.           $(this).parent_select_change(pro);  
  82.         });  
  83.     //});  
  84.   }  
  85. });  
  86.   
  87. $(document).ready(function(){  
  88.    $(this).show_province_items();   
  89. });  
  90. </script>  
  91.   
  92. </head>  
  93.     
  94. <body>  
  95.   <center>   
  96.  <h1>级联列表</h1>  
  97.   省:<select id='parent_select' name='province'></select>  市:<select id='child_select' name='city'></select>  
  98.   
  99.   <br>  
  100.   </center>  
  101. </body>  
  102. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值