js el-input输入框需要支持多输入 完整代码
时间: 2023-08-07 21:03:59 浏览: 198
好的,以下是一个支持多输入的 el-input 输入框的完整代码(使用了 Vue.js 和 Element UI):
```html
<template>
<div>
<el-input
:value="inputValue"
@input="handleInput"
@keyup.enter="handleEnter"
placeholder="请输入内容"
></el-input>
<ul>
<li v-for="(item, index) in inputList" :key="index">{{ item }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
inputValue: "", // 输入框的值
inputList: [] // 多输入的内容列表
};
},
methods: {
handleInput(value) {
// 监听输入框的输入事件
const lastChar = value[value.length - 1]; // 获取最后一个字符
if (lastChar === "," || lastChar === ",") {
// 如果是逗号或者中文逗号
const str = value.slice(0, -1); // 去除逗号
this.inputList.push(str); // 将输入框的值加入到列表中
this.inputValue = ""; // 清空输入框的值
}
},
handleEnter() {
// 监听回车事件
this.inputList.push(this.inputValue); // 将输入框的值加入到列表中
this.inputValue = ""; // 清空输入框的值
}
}
};
</script>
```
这个组件中,使用了 `inputValue` 和 `inputList` 两个 data 属性,分别表示输入框的值和多输入的内容列表。在 `handleInput` 方法中,监听输入框的输入事件,如果输入的最后一个字符是逗号或者中文逗号,则将输入框的值加入到列表中,并清空输入框的值;在 `handleEnter` 方法中,监听回车事件,将输入框的值加入到列表中,并清空输入框的值。最后,在模板中通过 v-for 渲染多输入的内容列表。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)