常见的vue省市区城市联动

<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 > 0 && 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>

城市数据:https://codepen.io/zhishaofei3-1471324920/pen/KqdGWa

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue三级联动可以通过使用Vue的数据绑定和监听功能来实现。首先,需要在Vue实例中定义相关的数据和方法。在引用\[1\]中的代码中,可以看到Vue实例中定义了pro、city、county和list这四个数据属性,分别表示选中的城市列表。同时,还定义了loadCity和loadCounty两个方法,用于根据选中的加载对应的城市域列表。 在HTML代码中,可以使用v-model指令将数据属性和下拉框进行绑定,实现数据的双向绑定。在引用\[2\]中的代码中,可以看到通过v-model指令将pro、city和county与对应的下拉框进行了绑定。同时,使用v-for指令遍历list数组,动态生成的选项。 在Vue实例的created钩子函数中,调用loadCity和loadCounty方法初始化选中的城市域。同时,使用watch监听pro和city的变化,当pro或city发生变化时,自动调用loadCity和loadCounty方法更新城市域的选项。 综上所述,通过Vue的数据绑定和监听功能,可以实现三级联动的效果。 #### 引用[.reference_title] - *1* *2* [Vue------实现三级联动](https://blog.csdn.net/weixin_52953038/article/details/126555237)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值