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

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

相关推荐
吴声子夜歌17 小时前
Vue3——脚手架Vite
前端·javascript·vue.js·vite
rADu REME18 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
被考核重击19 小时前
Vue响应式原理(下)
前端·javascript·vue.js
前端摸鱼匠1 天前
Vue 3 的v-bind合并行为:讲解v-bind与普通属性合并的规则
前端·javascript·vue.js·前端框架·ecmascript
Python私教2 天前
Pure-Admin-Thin 深度解析:完整版和精简版到底怎么选?
vue.js·人工智能·开源
ayqy贾杰2 天前
Cursor SDK发布!开发者可直接搬走其内核
前端·vue.js·面试
李白的天不白2 天前
vue 数据格式问题
前端·vue.js·windows
小白蒋博客2 天前
【ai开发段永平投资理财的知识图谱网站】第一天:搭 Vite + Vue 项目,跑通 Hello World
vue.js·人工智能·trae
@yanyu6662 天前
登录注册功能-明文
vue.js·springboot
滕青山2 天前
在线PDF拆分工具核心JS实现
前端·javascript·vue.js