admin管理员组文章数量:1391995
Using Vue 3, how can I dynamically set the selected
option when the data value does not match an available option value?
Condition:
If Florida (FL) is the stored data value for state, and the country is changed from United States (US) to Canada (CA), the State's option value bees blank. Instead, I would like for the placeholder item to show as the 'selected' option when there is no match.
<template>
<div>
<label v-if="data.country === 'US'">
State
<select v-model="data.state">
<option value="" disabled>state</option>
<option
v-for="(state, i) in states"
:value="state['code']"
:key="i"
>{{ state['name'] }}</option
>
</select>
</label>
<label v-if="data.country === 'CA'">
Province
<select v-model="data.state">
<option value="" disabled>province</option>
<option
v-for="(province, i) in provinces"
:value="province['code']"
:key="i"
>{{ province['name'] }}</option
>
</select>
</label>
</div>
<label>
Country
<select v-model="data.country">
<option value="" disabled>country</option>
<option
v-for="(country, i) in countries"
:value="country['code']"
:key="i"
>{{ country['name'] }}</option
>
</select>
</label>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import data from '...'
import states from '...'
import provinces from '...'
import countries from '...'
export default defineComponent({
setup() {
...
return { data, states, provinces, countries }
},
})
</script>
Using Vue 3, how can I dynamically set the selected
option when the data value does not match an available option value?
Condition:
If Florida (FL) is the stored data value for state, and the country is changed from United States (US) to Canada (CA), the State's option value bees blank. Instead, I would like for the placeholder item to show as the 'selected' option when there is no match.
<template>
<div>
<label v-if="data.country === 'US'">
State
<select v-model="data.state">
<option value="" disabled>state</option>
<option
v-for="(state, i) in states"
:value="state['code']"
:key="i"
>{{ state['name'] }}</option
>
</select>
</label>
<label v-if="data.country === 'CA'">
Province
<select v-model="data.state">
<option value="" disabled>province</option>
<option
v-for="(province, i) in provinces"
:value="province['code']"
:key="i"
>{{ province['name'] }}</option
>
</select>
</label>
</div>
<label>
Country
<select v-model="data.country">
<option value="" disabled>country</option>
<option
v-for="(country, i) in countries"
:value="country['code']"
:key="i"
>{{ country['name'] }}</option
>
</select>
</label>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import data from '...'
import states from '...'
import provinces from '...'
import countries from '...'
export default defineComponent({
setup() {
...
return { data, states, provinces, countries }
},
})
</script>
Share
Improve this question
edited Jul 8, 2021 at 23:47
Ryan Prentiss
asked Jul 8, 2021 at 21:25
Ryan PrentissRyan Prentiss
1,6623 gold badges30 silver badges51 bronze badges
1 Answer
Reset to default 3You could assign a blank value to the placeholder option
s:
<option value="" disabled>state</option>
<option value="" disabled>province</option>
And use a watcher on data.country
to set data.state
to the blank value when the country changes:
import { defineComponent, watch } from 'vue'
export default defineComponent({
setup() {
//...
watch(() => data.country, () => data.state = '')
},
})
demo
本文标签: javascriptVue 3 Dynamically set selected option of select form elementStack Overflow
版权声明:本文标题:javascript - Vue 3 Dynamically set selected option of select form element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744632882a2616646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论