The example in this article describes how to simply implement the city's secondary linkage selection plug-in using JS. Share it with everyone for your reference. The details are as follows:
The city linkage selection menu implemented by js is often seen on the Internet. I will not introduce it in detail. The prototype of this city selection menu is based on Select and is mainly implemented using JavaScript. It is made using basic techniques such as arrays and loops. This effect is just to demonstrate how to implement it. The data inside is incomplete. You can add it yourself if needed.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-ejld-city-cha-plug-codes/
The specific code is as follows:
<html> <head> <title>Js城市二级联动选择插件</title> <script> var citys=new Array( new Array("南京","淮安","扬州","常州",'其它'), new Array("北京"), new Array("天津"), new Array("上海"), new Array("其它") ); function scity(pname,cname){ var province=['江苏省','北京','天津','上海','其它']; document.write('<select id="pro" onchange="selectc(this)" name="'+pname+'">'); document.write('<option value="">--选择省份--</option>') for(var i=0;i<province.length;i++){ document.write('<option value="'+province[i]+'">'+province[i]+'</option>'); } document.write('</select>'); document.write('<select id="city" name="'+cname+'">'); document.write('<option value="">--选择城市--</option>'); document.write('</select>'); selectc(document.getElementById("pro")); } function selectc(pobj){ var index=pobj.selectedIndex-1; var cobj=document.getElementById("city"); cobj.innerHTML=''; if(index>=0){ for(var i=0;i<citys[index].length;i++){ var option=document.createElement("option"); var text=citys[index][i]; option.value=text; option.innerHTML=text; cobj.appendChild(option); } }else{ var option=document.createElement("option"); option.value=""; option.innerHTML="--选择城市--"; cobj.appendChild(option); } } </script> </head> <body> <script> scity('p','c'); </script> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.