element ui d的多选框可以在后面加一个其他然后自己输入内容详细案例
时间: 2024-03-29 11:40:17 浏览: 19
好的,下面是一个详细的案例,演示如何在Element UI的多选框中添加“其他”选项,并实现自定义输入内容的功能。
HTML代码:
```html
<template>
<div>
<el-checkbox-group v-model="checkedValues" @change="handleChange">
<el-checkbox label="apple"></el-checkbox>
<el-checkbox label="banana"></el-checkbox>
<el-checkbox label="orange"></el-checkbox>
<el-checkbox label="__other__">其他</el-checkbox>
</el-checkbox-group>
<el-input v-model="otherValue" v-if="showOther" placeholder="请输入其他内容"></el-input>
</div>
</template>
```
JS代码:
```js
<script>
export default {
data() {
return {
checkedValues: [], // 选中的值
otherValue: '', // 其他输入框中的值
showOther: false, // 是否显示其他输入框
};
},
methods: {
handleChange(val) {
if (val.includes('__other__')) {
// 如果选中了其他选项,显示其他输入框
this.showOther = true;
} else {
// 如果取消选中其他选项,隐藏其他输入框并清空值
this.showOther = false;
this.otherValue = '';
}
},
},
computed: {
checkboxOptions() {
// 处理多选框的选项列表,将其他选项的value值设置为一个特殊的字符串
return this.checkedValues.includes('__other__')
? this.checkedValues.map((item) => ({
label: item === '__other__' ? this.otherValue : item,
value: item === '__other__' ? this.otherValue : item,
}))
: this.checkedValues.map((item) => ({ label: item, value: item }));
},
},
};
</script>
```
在上面的代码中,我们首先在多选框的选项列表中添加了一个“其他”选项,并将其value值设置为“__other__”。然后在change事件回调函数中,判断是否选中了“其他”选项,如果选中了,则显示一个输入框供用户输入内容,如果取消选中,则隐藏输入框并清空值。
在computed中,我们处理多选框的选项列表,将其他选项的value值设置为用户输入的内容,并在显示选项时将value值替换为对应的文本。
这样,就实现了在Element UI的多选框中添加“其他”选项,并实现自定义输入内容的功能。