element-ui输入框换行符占位问题处理

遇到的问题是在我换行的时候,例如我第一行输入了20个字符,这是我限制的的,换行后什么都没有输入但是已经计入到了21个字符长度。这里的96个字符后 就无法继续输入了,因为换行符占用导致的。

html 复制代码
  <el-input
            v-model="textarea"
            type="textarea"
            :rows="7"
            resize="none"
            placeholder="最多只能输入5行,每行最多20个汉字"
            @input="handleInputOne"
          />
          <div class="char-counter">{{ actualCharCountOne }}/{{ maxTotalLengthOne }}</div>
javascript 复制代码
 data() {
    return {
      textarea: '',
      maxLineLengthOne: 20, // 一行最多输入字符
      maxLines: 5 // 最大行数
  }
},

 computed: {
    maxTotalLengthOne() {
      return this.maxLineLengthOne * this.maxLines
    },
    // 计算实际字符数(不包括换行符)
    actualCharCountOne() {
      return this.textarea ? this.textarea.replace(/[\r\n]/g, '').length : 0
    },
  },
  methods: { 
        // 修改后的 limitText 方法
    limitText(value, maxLineLength, maxLines, fieldName, maxTotalChar = null) {
      if (!value) return

      // 统一换行符为 \n
      const normalizedValue = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
      let lines = normalizedValue.split('\n')
      let hasChange = false

      // 限制每行字符数(不包括换行符)
      for (let i = 0; i < lines.length; i++) {
        if (lines[i].length > maxLineLength) {
          lines[i] = lines[i].substring(0, maxLineLength)
          hasChange = true
        }
      }

      // 限制最大行数
      if (lines.length > maxLines) {
        lines = lines.slice(0, maxLines)
        hasChange = true
      }

      // 限制总字符数(不包括换行符)
      if (maxTotalChar !== null) {
        let currentCharCount = 0
        const validLines = []

        for (let i = 0; i < lines.length; i++) {
          const line = lines[i]
          // 只计算实际字符数,不包括换行符
          if (currentCharCount + line.length <= maxTotalChar) {
            validLines.push(line)
            currentCharCount += line.length
          } else {
          // 如果当前行会超出限制,截取当前行
            const remainingChars = maxTotalChar - currentCharCount
            if (remainingChars > 0) {
              validLines.push(line.substring(0, remainingChars))
            }
            hasChange = true
            break
          }
        }

        if (validLines.length !== lines.length) {
          lines = validLines
          hasChange = true
        }
      }

      if (hasChange) {
        const newValue = lines.join('\n')
        if (fieldName === 'opera_step') {
          this.magneticArmData.opera_step = newValue
        } else {
          this[fieldName] = newValue
        }
      }
    },

    // 工艺补充说明的限制
    handleInputOne(value) {
      this.limitText(value, this.maxLineLengthOne, this.maxLines, 'textarea', this.maxTotalLengthOne)
    },
 }

最后实现的效果 输入字符是剔除掉换行符的

相关推荐
老男孩游戏时光3 小时前
Vue3 Composition API 最佳实践,别再乱写 setup 了
vue.js
+VX:Fegn08953 小时前
计算机毕业设计|基于java+ vue共享单车信息系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Bs_MoneyMagnet3 小时前
基于springboot+vue的非遗物质文化遗产系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
计算机毕设定制辅导-无忧学长4 小时前
《基于SpringBoot的未成年人健康知识科普平台的设计与实现》
java·开发语言·vue.js·spring boot·未成年人健康知识科普平台
专业程序开发源6 小时前
springboot篮球联赛管理系统13635-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·php·课程设计
一 乐6 小时前
自习室座位预约管理系统|基于springboot + vue自习室座位预约管理系统(源码+数据库+文档)
java·vue.js·spring boot
晚安日记wanna7 小时前
Vue2 的 defineProperty 差在哪四层追问筛掉九成候选人
前端·vue.js·面试
dy171719 小时前
vue2树节点刷新后定位到上一节点展开
vue.js
高级程序源1 天前
django大学生创新创业项目管理系统94923-计算机课程设计、毕业设计
javascript·vue.js·spring boot·后端·python·django·课程设计
VXbishe1 天前
【课程设计】基于SpringBoot的乡村政务服务系统的设计与实现-计算机毕设77011
javascript·vue.js·spring boot·python·php·课程设计·政务