【问题标题】:How to handle multiple on change events on drop-down selection in vuejs如何在 vuejs 中的下拉选择中处理多个更改事件
【发布时间】:2018-01-28 05:41:16
【问题描述】:

我有一个问题,用户需要先选择国家,然后是州,然后是城市。现在我已经在国家和州上设置了@change 事件,一旦用户选择国家,它就会获取州,并且也会触发州上的@change,并且由于我有 state_id = '',现在我的 getCities 函数失败了。

<select v-model="country_id" @change="getStates()">
    <option v-for="country in countries" :value="country.id">{{ country.name }}</option>
</select>
<select v-model="state_id" @change="getCities()">
    <option v-for="state in states" :value="state.id">{{ state.name }}</option>
</select>

data(){
    return{
        countries: [],
        states: [],
        cities: [],
        country_id: '',
        state_id: '',
        city_id: ''
    }
},
mounted(){
    getCountries();
},
methods:{
    getCountries(){
        // this.countries = response.data
    }
    geStates(){
        //get states where country_id = this.country_id
    },
    getCities(){
        //get cities where state_id = this.state_id
        //once country is selected even this is getting triggered
    }

}

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    在实际获取数据之前检查状态是否有值。

    getCities(){
        // Check to see if state_id has a value
        if (!this.state_id) return
    
        //get cities where state_id = this.state_id
        //once country is selected even this is getting triggered
    }
    

    【讨论】:

    • 是的,我这样做了。但想知道 vuejs 是否有 on selected 的事件。
    • @JagadeshaNH change 基本上处于选中状态。在这种情况下,Vue 只是使用原生方法。
    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    相关资源
    最近更新 更多