gpt4 book ai didi

javascript - 在 vuejs 2 中动态更改选择输入选项

转载 作者:搜寻专家 更新时间:2023-11-01 04:59:52 25 4
gpt4 key购买 nike

如何动态更改选择下拉 v-model 中的选项?

我有 2 个选择输入,一个应该根据其他输入进行更改。

例如,如果我选择“水果”,则选择显示水果,如果我选择“蔬菜”,则显示蔬菜。

最佳答案

我不使用Vuejs,但是看了文档之后:

var TypesArr = {
Fruit: [{ text: 'Apple', value: 'Apple' }, { text: 'Orange', value: 'Orange' }, { text: 'Mango', value: 'Mango' }],
Meat: [{ text: 'Steak', value: 'Steak' }, { text: 'Pork', value: 'Pork' }]
}


var selectTwo = new Vue({
el: '#select2',
data: {
selected: TypesArr['Fruit'][0],
options: TypesArr['Fruit']
},
methods: {
update: function (value)
{
this.options = TypesArr[value]
}
}
})


new Vue({
el: '#select1',
data: {
selected: 'Fruit',
options: [ { text: 'Fruit', value: 'Fruit' }, { text: 'Meat', value: 'Meat' } ]
},
methods: {
onChange: function (event)
{
selectTwo.update(event.srcElement.value)
}
}
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<select v-on:change="onChange" id="select1">
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
</select>

<select id="select2">
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
</select>

关于javascript - 在 vuejs 2 中动态更改选择输入选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40438924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com