利用Vue实现简单的省市县联动

<!doctype html>
<html lang="en">

<head>
  <title>省市县</title>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <!-- 内网Vue.js -->
  <script src="D:\WebCode\Vue.js"></script>
  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
  <!-- 容器 -->
  <div id="app" class="container">
    <!-- 行 -->
    <div class="col-lg-4">
      <!-- 下拉框 -->
      <div class="form-group">
        <label for="">省</label>
        <select 
        v-model="province" 
        class="form-control" name="" id="">
          <option v-for="(item, index) in provinceList" :key="index"  :value="index">{{item}}</option>

        </select>
      </div>
    </div>
    <div class="col-lg-4">
      <label for="">市</label>
      <select v-model="city" class="form-control" name="" id="">
        <option v-for="(item, index) in a2" :key="index"  :value="index">{{item}}</option>

      </select>

    </div>
    <div class="col-lg-4">
      <label for="">县</label>
      <select v-model="county" class="form-control" name="" id="">
        <option v-for="(item, index) in a3" :key="index"  :value="index">{{item}}</option>

      </select>

    </div>
  </div>

  <script>
  let vm=new Vue({
    el:'#app',
    data: {
      county:'',
      city:'',
      province:'',
      provinceList:["湖北省", "湖南省", "广东省"],
      citylist:[["武汉市", "宜昌市", "咸宁市"], ["长沙市", "常德市", "邵阳市"], ["广州市", "深圳市", "惠州市"]],
      countyList: [[["武昌区", "洪山区"], ["夷陵区", "西陵区"], ["咸安区"]], 
      [["芙蓉区", "雨花区", "岳麓区"], ["鼎城区"], ["双清区"]], [["荔湾区", "白云区"], ["福田区","保安区","龙岗区"], ["惠阳区"]]],

    },
    computed: {
      a2:function(){
       
        return this.citylist[this.province];
      },
      a3:function(){
        if(this.city!=='')
        return this.countyList[this.province][this.city];
      }

    },
    
  })
  </script>


  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
    integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
    crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
    integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
    crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
    crossorigin="anonymous"></script>
</body>

</html>
 
发布了12 篇原创文章 · 获赞 1 · 访问量 195

猜你喜欢

转载自blog.csdn.net/weixin_44364444/article/details/103899293