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)
    },
 }

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

相关推荐
一 乐12 分钟前
汽车租赁|基于SprinBoot+vue的汽车租赁管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·汽车·论文·毕设·汽车租赁管理系统
MaCa .BaKa4 小时前
55-宠物爱心救助领养系统-宠物救助领养系统
java·vue.js·tomcat·maven·springboot·宠物救助领养系统
海鸥两三5 小时前
基于 Vue 3 + 高德地图的网格规划系统实战(有源码)
前端·javascript·vue.js
专注VB编程开发20年6 小时前
我制作excel工作簿的选项卡,发给deep seek, 昨天修改了一天
前端·vue.js·excel
qq_420362037 小时前
前端国际化方案
前端·javascript·vue.js·国际化·reactjs
香香爱编程7 小时前
vue3自定义顶部弹窗
前端·javascript·vue.js
蜡台8 小时前
Vue Echart 的 **高阶组件化** 封装思路
前端·javascript·vue.js·echarts
xuankuxiaoyao8 小时前
vue.js 路由第二篇
前端·javascript·vue.js
一 乐8 小时前
图书电子商务网站系统|基于SprinBoot+vue图书电子商务网站设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·图书电子商务网站系统
zhangyao94033019 小时前
开发pc端时,表格的高度怎么设置才能铺满页面
前端·javascript·elementui