【发布时间】:2021-04-29 12:35:59
【问题描述】:
我有一个简单的应用程序,它在生成两个选择标记的select 语句中使用v-for。创建select 语句的groupedSKUAttributes 变量如下所示:
groupedSKUAttributes = {colour: [{id: 1, name: 'colour', value: 'red'},
{id: 2, name: 'colour', value: 'blue'}],
size: [{id: 3, name: 'size', value: '40'},
{id: 4, name: 'size', value: '42'}]}
我还有一个按钮,我想清除 select 字段。 如何获得clear 方法以使每个select 字段选择其默认<option value='null' selected>select a {{ attributeName }}</option> 值?我不知道我是否打算使用v-model这里是groupedSKUAttributes。任何建议将不胜感激。
模板如下所示:
<template>
<div>
<select
v-for='(attribute, attributeName) in groupedSKUAttributes'
:key='attribute'
@change='update(attributeName, $event.target.value)'>
<option value='null' selected>select a {{ attributeName }}</option>
<option
v-for='a in attribute'
:value='a.id'
:label='a.value'
:key='a.id'>
</option>
</select>
</div>
<button @click='clear'>clear</button>
</template>
JS 脚本是这样的:
<script>
export default {
name: 'app',
data() {
return {
groupedSKUAttributes: null,
}
},
methods: {
clear() {
console.log('clear');
},
update(attributeName, attributeValue) {
console.log(attributeName, attributeValue);
},
getSKUAttributes() {
API
.get('/sku_attribute/get')
.then((res) => {
this.skuAttributes = res.data;
this.groupedSKUAttributes = this.groupBy(this.skuAttributes, 'name');
})
.catch((error) => {
console.error(error);
});
},
},
created() {
this.getSKUAttributes();
}
}
</script>
【问题讨论】: