最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue 3 Dynamically set selected option of select form element - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 3

You could assign a blank value to the placeholder options:

<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

发布评论

评论列表(0)

  1. 暂无评论