JavaScript开发三级联动之准备工作
前端做好了,接下来就是我们的JavaScript了,在写之前我们要将我们所需要的省市区的数组先定义好,如下所示:
<script> //声明省 var oProc = ["安徽","上海","山东"]; //直接声明array //声明市 var oCity = [ ["合肥","淮南","芜湖"], ["浦东","闵行","浦西"], ["济南","青岛","枣庄"] ]; //声明区 var oDist = [ [ ["政务区","庐阳区","蜀山区"], ["田家庵区","大通区","九龙岗区"], ["镜湖区","鸠江区","三山区"] ], [ ["浦东1","浦东2","浦东3"], ["闵行1","闵行2","闵行3"], ["浦西1","浦西","浦西3"] ], [ ["历下区","天桥区","长清区"], ["市南区","市北区","李沧区"], ["薛城区","市中区","峄城区"] ] ]; </script>
这只是个简单的举个例子,当真正的工作中用到的可能就是全省,全国,那么工作量就比较大,这就是我说的准备工作。但是不用担心,这个都会有其他程序员写好的例子,到时候我们直接拿来用就可以了。这里只是让大家知道。
用数组的形式声明省,市,区,然后再去使用。