[英]Select different values on two select tags in vuejs
I'm a newbie to VueJS and I'm trying to build a page with two select options of countries.我是 VueJS 的新手,我正在尝试使用两个国家/地区的 select 选项构建一个页面。 The options cannot have the same value at the same time.
选项不能同时具有相同的值。
This is my component file:这是我的组件文件:
<template>
<div>
<form @change="onChange">
<select name="originCountries" id="originCountries" v-model="initialOriginCountry">
<option value="BRAZIL">BRAZIL</option>
<option value="USA">USA</option>
<option value="ARGENTINA">BRAZIL</option>
</select>
<div>
not important info...
</div>
<select name="destinyCountries" id="destinyCountries" v-model="initialDestinyCountry">
<option value="BRAZIL">BRAZIL</option>
<option value="USA">USA</option>
<option value="ARGENTINA">BRAZIL</option>
</select>
</form>
</div>
</template>
<script lang='ts'>
import { Component, Vue } from 'vue-property-decorator'
@Component
export default class FormSubmitter extends Vue {
private initialDestinyCountry = 'USA'
private initialOriginCountry = 'BRAZIL'
}
So, the top select option starts with 'ÚSA', if I change this to 'BRAZIL', the bottom select option needs to change to 'USA'因此,顶部 select 选项以“ÚSA”开头,如果我将其更改为“巴西”,则底部 select 选项需要更改为“美国”
I've spent all day in this and conclud the problem is the fact I cannot track what select option is the origin and what is the destiny.我花了一整天的时间,并得出结论,问题是我无法追踪 select 选项的起源和命运。
If I start selecting the top select option, this is the origin.如果我开始选择顶部的 select 选项,这就是原点。 So the bottom select option is destiny.
所以底部的 select 选项是命运。 If I start selecting the bottom select option, this is the origin.
如果我开始选择底部的 select 选项,这就是原点。 So the top select option is destiny.
所以顶级 select 选项是命运。
So I was trying to do this inside onChange function with if else.所以我试图在 onChange function 中使用 if else 来执行此操作。 I created handle variables to keep the initial origin and destiny values, but this got confusing too fast.
我创建了句柄变量来保持初始原点和命运值,但这太快了。
Is there any way to do this in a cleaner way using vuejs?有没有办法使用 vuejs 以更清洁的方式做到这一点?
Using Vue 2 with the Vue CLI, I created a sample Single File Component 'TwoSelectTags.vue' that should give you some ideas for managing your select tags.使用 Vue 2 和 Vue CLI,我创建了一个示例单文件组件“TwoSelectTags.vue”,它应该为您提供一些管理 select 标签的想法。
<template>
<div class="two-select-tags">
<div class="col-md-4">
<form @submit.prevent="submitForm">
<div class="form-group">
<label for="originCountries">Origin Country</label>
<select id="originCountries" class="form-control"
v-model="originCountry" @change="changeOrigin">
<option v-for="(countryOption, index) in countryOptions" :key="index">{{ countryOption }}</option>
</select>
<span>Selected origin: {{ originCountry }}</span>
</div>
<div class="form-group">
<label for="destinationCountries">Destiny Country</label>
<select id="destinationCountries" class="form-control"
v-model="destinationCountry" @change="changeDestination">
<option v-for="(countryOption, index) in countryOptions" :key="index">{{ countryOption }}</option>
</select>
<span>Selected destination: {{ destinationCountry }}</span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
countryOptions: [
'BRAZIL',
'USA',
'ARGENTINA'
],
originCountry: 'USA',
destinationCountry: 'BRAZIL'
}
},
methods: {
changeOrigin() {
// Add logic to handle destination options
if (this.originCountry === 'BRAZIL') {
this.destinationCountry = 'USA';
}
},
changeDestination() {
// Add logic to handle origin options
},
submitForm() {
console.log('submitForm');
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.