JavaScript開發三級連動之準備工作

前端做好了,接下來就是我們的JavaScript了,在寫之前我們要將我們所需要的省市區的數組先定義好,如下所示:

<script>
   //声明省
   var oProc = ["安徽","上海","山东"];  //直接声明array
   //声明市
   var oCity = [
       ["合肥","淮南","芜湖"],
       ["浦东","闵行","浦西"],
       ["济南","青岛","枣庄"]
   ];
   //声明区
   var oDist = [
       [
           ["政务区","庐阳区","蜀山区"],
           ["田家庵区","大通区","九龙岗区"],
           ["镜湖区","鸠江区","三山区"]
       ],
       [
           ["浦东1","浦东2","浦东3"],
           ["闵行1","闵行2","闵行3"],
           ["浦西1","浦西","浦西3"]
       ],
       [
           ["历下区","天桥区","长清区"],
           ["市南区","市北区","李沧区"],
           ["薛城区","市中区","峄城区"]
       ]
   ];
</script>

這只是個簡單的舉個例子,當真正的工作中用到的可能就是全省,全國,那麼工作量就比較大,這就是我說的準備工作。但不用擔心,這都會有其他程式設計師寫好的例子,到時候我們直接拿來用就好了。這裡只是讓大家知道。

用陣列的形式宣告省,市,區,然後再去使用。

繼續學習
||
<script> //声明省 var oProc = ["安徽","上海","山东"]; //直接声明array //声明市 var oCity = [ ["合肥","淮南","芜湖"], ["浦东","闵行","浦西"], ["济南","青岛","枣庄"] ]; //声明区 var oDist = [ [ ["政务区","庐阳区","蜀山区"], ["田家庵区","大通区","九龙岗区"], ["镜湖区","鸠江区","三山区"] ], [ ["浦东1","浦东2","浦东3"], ["闵行1","闵行2","闵行3"], ["浦西1","浦西","浦西3"] ], [ ["历下区","天桥区","长清区"], ["市南区","市北区","李沧区"], ["薛城区","市中区","峄城区"] ] ]; </script>