el-input文本域字数限制,超过显示变红并禁止输入

效果展示
在这里插入图片描述
代码:

  <el-form-item label="养护要点说明:" prop="remark" :rules="[{ required: true, message: '不能为空' }]">
            <el-input
              type="textarea"
              @input="timeEffectInput"  // 输入事件(在这处理限制)
              maxlength="200"  // 最大字数限制
              resize="none"    // 控制是否能被用户缩放
              show-word-limit  // 是否显示输入字数统计
              :rows="3"        // 自适应内容高度,可传入对象,如,{ minRows: 2, maxRows: 6 }
              style="width: 92%"
              v-model="dialogForm.remark"
              placeholder="请输入养护要点说明!"
            ></el-input>
          </el-form-item>
timeEffectInput() {
      const length = this.dialogForm.remark.length  // 先拿到当前输入时文本域文字长度
      if (length >= 200) {  // 当长度超过200
      	// 获取文本域计数统计设置字体为红色
        this.$el.querySelector('.el-input__count').style = 'color:red !important' 
       	// 防止后续输入溢出截取0-200的字符
        this.dialogForm.remark = this.dialogForm.remark.substr(0, 200)
      } else {
        // 不超过则设置为原字体颜色
        this.$el.querySelector('.el-input__count').style = 'color:#909399 !important'
      }
    },
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值