Coder Social home page Coder Social logo

cityselectdemo's Introduction

城市三级联动选择,省市区demo

Vue + element 三级联动,可高度定制

选择框代码

 <div class="city">
     <el-select v-model="province" placeholder="选择省份" @change="getCity(province)">
       <el-option
           v-for="(item,index) in provinceData"
           :key="index"
           :label="item.name"
           :value="item.id">
       </el-option>
     </el-select>
 
     <el-select v-model="city" placeholder="选择城市" @change="getDistrict(city)">
       <el-option
           v-for="(item,index) in cityData"
           :key="index"
           :label="item.name"
           :value="item.id">
       </el-option>
     </el-select>
 
     <el-select v-model="district" placeholder="选择区县">
       <el-option
           v-for="(item,index) in districtData"
           :key="index"
           :label="item.name"
           :value="item.id">
       </el-option>
     </el-select>
   </div>

功能代码

    data(){
      return {
        province: '',  // 省份
        provinceData: '', // 省份列表

        city: '',  // 城市
        cityData: '',  // 城市列表

        district: '', // 区县
        districtData: '', // 区县列表

      }
    },
    created(){
      this.getProvinceData()
    },
    methods:{
      getProvinceData(){
        this.$axios.get('/static/mock/city.json')
            .then(res =>{
              this.provinceData = res.data
            })
      },

      // 获取城市
      getCity(val){
        this.city = ''
        this.district = ''
        this.provinceData.forEach(res =>{
          if(res.id === val){
            this.cityData = res.child
          }
        })
      },

      // 获取区县
      getDistrict(val){
        this.district = ''
        this.cityData.forEach(res =>{
          if(res.id === val){
            this.districtData = res.child
          }
        })
      },
    }

cityselectdemo's People

Contributors

wishqaq avatar wishtime avatar

Watchers

 avatar  avatar

Forkers

llxgg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.