码迷,mamicode.com
首页 > 其他好文 > 详细

vue 选城市三级联动

时间:2017-06-08 21:24:14      阅读:457      评论:0      收藏:0      [点我收藏+]

标签:targe   code   remount   tar   city   type   date   aof   blog   

<div id="example">
    <select v-model="prov">
        <option v-for="option in arr" :value="option.name">
            {{ option.name }}
        </option>
    </select>
    <select v-model="city">
        <option v-for="option in cityArr" :value="option.name">
            {{ option.name }}
        </option>
    </select>
    <select v-model="district" v-if="district">
        <option v-for="option in districtArr" :value="option.name">
            {{ option.name }}
        </option>
    </select>
</div>

<script type="text/javascript">
    var vm = new Vue({
        el: #example,
        data: {
            arr: arrAll,
            prov: 北京,
            city: 北京,
            district: 东城区,
            cityArr: [],
            districtArr: []
        },
        methods: {
            updateCity: function () {
                for (var i in this.arr) {
                    var obj = this.arr[i];
                    if (obj.name == this.prov) {
                        this.cityArr = obj.sub;
                        break;
                    }
                }
                this.city = this.cityArr[1].name;
            },
            updateDistrict: function () {
                for (var i in this.cityArr) {
                    var obj = this.cityArr[i];
                    if (obj.name == this.city) {
                        this.districtArr = obj.sub;
                        break;
                    }
                }
                if(this.districtArr && this.districtArr.length > 1 && this.districtArr[1].name) {
                    this.district = this.districtArr[1].name;
                } else {
                    this.district = ‘‘;
                }
            }
        },
        beforeMount: function () {
            this.updateCity();
            this.updateDistrict();
        },
        watch: {
            prov: function () {
                this.updateCity();
                this.updateDistrict();
            },
            city: function () {
                this.updateDistrict();
            }
        }
    })
</script>

 

城市json数据量比较大,完整案例请看:

vue 选城市三级联动

标签:targe   code   remount   tar   city   type   date   aof   blog   

原文地址:http://www.cnblogs.com/zhishaofei/p/6964673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!