【发布时间】:2018-04-29 08:40:23
【问题描述】:
我在一个表单中有两个 custom select input 字段:Country 和 City。正如您可能已经猜到的那样,城市取决于国家。所以当用户选择一个国家时:
- 执行 ajax 调用
- 获取所选国家/地区的城市
- 获取的城市显示在第二个选择框中
场景:从国家选择框中,我选择了美国。从城市选择框中,我选择了德克萨斯(其值为:6)。现在如果我回到第一个选择框,将国家更改为英国,它将根据之前的选择自动选择英国的第 6 个城市。
这是我正在做的:
<custom-select type="select" name="country_id" @change="selectCountry">
<option disabled selected>Choose a Country</option>
<option v-for="country in countries" :value="country.id">@{{ country.name }}</option>
</custom-select>
<custom-select type="select" name="city_id" @change="selectCity">
<option disabled selected>Choose a City</option>
<option v-for="city in cities" :value="city.id">@{{ city.name }}</option>
</custom-select>
如何在每次选择国家时重置城市选择?
【问题讨论】:
-
只需在
selectCountry函数中重置城市值... -
如何重置?
-
这完全取决于您的
selectCountry以及您为\custom-select使用的变量 -
在我的 selectCountry() 中,我正在执行 axios 调用以获取城市。在自定义选择组件中,我没有额外的变量。
-
然后包括所有相关数据。
标签: javascript vue.js