标签:
实现效果:
默认情况,在未选择省份时,城市里面无选项。当选择省份之后,城市下拉框里出现对应的城市。
HTML结构如下:
1 <form action=""> 2 <fieldset id=""> 3 <legend>选择所在的省份及城市</legend> 4 <span>省份:</span> 5 <select name="province" id="province"> 6 <option value="0">--请选择--</option> 7 </select> 8 <span>城市:</span> 9 <select name="city" id="city"> 10 <option value="0">--请选择--</option> 11 </select> 12 </fieldset> 13 </form>
JS代码如下:
1 var prov = [‘北京‘,‘山东省‘,‘湖北省‘,‘甘肃省‘]; 2 var city1 = [‘北京‘]; 3 var city2 = [‘济南‘,‘烟台‘,‘青岛‘,‘潍坊‘,‘威海‘,‘太原‘]; 4 var city3 = [‘武汉‘,‘孝感‘,‘黄冈‘]; 5 var city4 = [‘银川‘,‘兰州‘]; 6 var province = $(‘#province‘); 7 var city = $(‘#city‘); 8 for(var i=0; i < prov.length; i++){ 9 province.get(0).add(new Option(prov[i],i+1)); 10 } 11 province.bind(‘change‘,select_city); 12 function select_city(){ 13 city.get(0).options.length = 1; 14 var a = parseInt(province.val()); 15 var cur_city; 16 switch(a){ 17 case 1: 18 cur_city = city1; 19 break; 20 case 2: 21 cur_city = city2; 22 break; 23 case 3: 24 cur_city = city3; 25 break; 26 case 4: 27 cur_city = city4; 28 break; 29 default: 30 cur_city = null; 31 } 32 for(var i=0; i < cur_city.length; i++){ 33 city.get(0).add(new Option(cur_city[i],i+1)); 34 } 35 }
标签:
原文地址:http://www.cnblogs.com/shifan/p/5613795.html