1. 程式人生 > >php+vue.js 實現省市縣鄉的四級聯動(ajax載入)

php+vue.js 實現省市縣鄉的四級聯動(ajax載入)

(注:需要SQL的朋友請在評論區留下email)

除了引入vue.js還需要引入vue的一個ajax庫:vue-resource

html:

  1. <!DOCTYPE html>
  2. <html>
  3. <headlang="en">
  4.     <metacharset="UTF-8">
  5.     <metaname="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  6.     <title></title>
  7. </
    head>
  8. <scriptsrc="../vue.js"></script>
  9. <scriptsrc="../vue-resource.min.js"></script>
  10. <style>
  11.     select{  
  12.         height: 2em;  
  13.         width: 12em;  
  14.         text-align: center;  
  15.     }  
  16. </style>
  17. <body>
  18. <divid="address"style="overflow: scroll;border: 1px solid #000000;height: 15em;text-align: center"
    >
  19.     <p>
  20.         <selectname="province"v-on:change="getAddress($event)">
  21.                <optionv-for="pro in province"v-bind:value="pro.id">{{pro.name}}</option>
  22.         </select>
  23.     </p>
  24.     <p>
  25.         <selectname="city"v-on:change="getAddress($event)">
  26.                 <
    optionv-for="ci in city"v-bind:value="ci.id">{{ci.name}}</option>
  27.         </select>
  28.     </p>
  29.     <p>
  30.         <selectname="county"v-on:change="getAddress($event)">
  31.             <optionv-for="cou in county"v-bind:value="cou.id">{{cou.name}}</option>
  32.         </select>
  33.     </p>
  34.     <p>
  35.         <selectname="town">
  36.             <optionv-for="to in town"v-bind:value="to.id">{{to.name}}</option>
  37.         </select>
  38.     </p>
  39. </div>
  40. <script>
  41.     //後臺傳來的初始值  
  42.     window.onload = function(){  
  43.         address.$http.get('getAddress.php').then(function(response){  
  44.             address.province = address.province.concat(response.data);  
  45.         });  
  46.     };  
  47.     var address = new Vue({  
  48.         el:"#address",  
  49.         data: {  
  50.             province: [{"id":0,"name":'———所在省———'}],  
  51.             city:[{"id":0,"name":'———所在市———'}],  
  52.             county:[{"id":0,"name":'———所在區縣———'}],  
  53.             town:[{"id":0,"name":'———所在鎮鄉———'}]  
  54.         },  
  55.         methods: {  
  56.             getAddress: function (enevt) {  
  57.                 var DOMobj = enevt.currentTarget;  
  58.                 //獲取當前改變項  
  59.                var  thisName =  DOMobj.getAttribute("NAME");  
  60.                 var data= {};  
  61.                 var id = DOMobj.value;  
  62.                 switch (thisName){  
  63.                     case 'province':  
  64.                         this.city = [{"id": 0, "name": '———所在市———'}];  
  65.                         this.county = [{"id": 0, "name": '———所在區縣———'}];  
  66.                         this.town = [{"id": 0, "name": '———所在鎮鄉———'}];  
  67.                         data={'pro_id':id};  
  68.                         break;  
  69.                     case 'city':  
  70.                         this.county = [{"id": 0, "name": '———所在區縣———'}];  
  71.                         this.town = [{"id": 0, "name": '———所在鎮鄉———'}];  
  72.                         data={'city_id':id};  
  73.                         break;  
  74.                     case 'county':  
  75.                         this.town = [{"id": 0, "name": '———所在鄉———'}];  
  76.                         data={'county_id':id};  
  77.                         break;  
  78.                 }  
  79.                 var pro_id = DOMobj.value;  
  80.                 if (pro_id != 0) {  
  81.                     address.$http.get('getAddress.php', data).then(function (response) {  
  82.                       if(response.data.length>0){  
  83.                           switch (thisName){  
  84.                               case 'province':address.city = address.city.concat(response.data);break;  
  85.                               case 'city':address.county = address.county.concat(response.data);break;  
  86.                               case 'county':address.town = address.town.concat(response.data);break;  
  87.                           }  
  88.                       }  
  89.                     });  
  90.                 }  
  91.             }  
  92.         }  
  93.     });  
  94. </script>
  95. </body>
  96. </html>

getAddress.PHP

  1. <?php
  2. $dsn = 'mysql:dbname=zyytest;host=127.0.0.1';  
  3. $user = 'root';  
  4. $pwd ='root';  
  5. try{  
  6.     $mypdo = new PDO($dsn,$user,$pwd);  
  7. }catch (PDOException $e){  
  8.     throw new Exception($e->getMessage());  
  9. }  
  10. if(empty($_GET)){   //省  
  11.     $pro = $mypdo->query('select * from t_province');  
  12.     $pro = $pro->fetchAll(PDO::FETCH_ASSOC);  
  13.     exit(json_encode($pro));  
  14. }elseif(isset($_GET['pro_id'])){   //市  
  15.     $cirySql = $mypdo->prepare('select * from t_city where province_id = :pro_id ');  
  16.     $cirySql->execute(array("pro_id"=>$_GET['pro_id']));  
  17.     $city = $cirySql->fetchAll(PDO::FETCH_ASSOC);  
  18.     exit(json_encode($city));  
  19. }elseif(isset($_GET['city_id'])){      //縣  
  20.     $cirySql = $mypdo->prepare('select * from t_county where city_id = :pro_id ');  
  21.     $cirySql->execute(array("pro_id"=>$_GET['city_id']));  
  22.     $city = $cirySql->fetchAll(PDO::FETCH_ASSOC);  
  23.     exit(json_encode($city));  
  24. }elseif(isset($_GET['county_id'])){   //鄉  
  25.     $cirySql = $mypdo->prepare('select * from t_town where county_id = :pro_id ');  
  26.     $cirySql->execute(array("pro_id"=>$_GET['county_id']));  
  27.     $city = $cirySql->fetchAll(PDO::FETCH_ASSOC);  
  28.     exit(json_encode($city));  
  29. }else{  
  30.     exit();