项目中有时候需求是这样的?
输入框的内容设置好,提交后,再次编辑的时候只能添加不能删除原有的功能?
可以采用一个比较简易的,网上的一些方法过于繁琐!无需区分vue、react框架限制,采用原生实现比较简单点,实现思路:引入一个中间量,记录数据,根据length来做判断!!!废话不多说,上代码!!!!
<template>
<div>
<div class="task-form-item-box">
<input
type="text"
v-model="value"
@keydown="down"
placeholder="以逗号隔开"
class="task-form-item-ipn"
/>
</div>
<el-button @click="handleOk" type="primary" style="margin: 10px; float: left"
>提交</el-button
>
</div>
</template>
<script>
export default {
data() {
return {
value: "",
oldValue: "",
};
},
methods: {
down(e) {
let old = this.oldValue.length;
let nval = this.value.length;
if (nval <= old) {
if (e.keyCode === 8) {
e.preventDefault();
}
}
},
handleOk() {
this.oldValue = this.value;
},
},
};
</script>
<style lang="scss" scoped>
.task-form-item-box {
width: 200px;
height: 35px;
line-height: 35px;
border: #dcdfe6 1px solid;
padding-left: 12px;
box-sizing: border-box;
border-radius: 4px;
}
.task-form-item-ipn {
width: 180px;
height: 25px;
border: none;
outline: none;
}
.task-form-item-ipn::-webkit-input-placeholder {
/* WebKit browsers */
color: #c0c4cc;
font-size: 12px;
}
</style>