js实现省市级联效果分享
程序员文章站
2023-02-23 23:09:35
本文实例为大家分享了js实现省市级联效果的具体代码,供大家参考,具体内容如下
<...
本文实例为大家分享了js实现省市级联效果的具体代码,供大家参考,具体内容如下
<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> </head> <script type="text/javascript"> var shengarr =new array(); shengarr["广东"]=["广州","深圳","珠海","汕头","韶关"] ; shengarr["湖南"]=["长沙","张家界","株洲","怀化","常德"] ; shengarr["湖北"]=["武汉","荆州","宜昌","黄冈","仙桃"] ; shengarr["安徽"]=["合肥","黄山"] ; shengarr["河南"]=["郑州","信阳","洛阳"] ; function getsheng(){ var s =document.getelementbyid("sheng"); for(var v in shengarr){ s.add(new option(v,v),null); } } function getcity(){ var s =document.getelementbyid("sheng"); var c =document.getelementbyid("city"); var v=s.value; c.options.length =0; for(var i in shengarr[v]){ c.add(new option(shengarr[v][i],shengarr[v][i]),null); } } </script> <!--页面加载时获取 省名--> <body onload="getsheng()"> <!-- 改变省时匹配该省的城市名--> 省:<select id="sheng" onchange="getcity()"> <option>--请选择</option> </select> 市:<select id="city"></select> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。