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

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

相关推荐
东东516几秒前
OA自动化居家办公管理系统 ssm+vue
java·前端·vue.js·后端·毕业设计·毕设
Irene199123 分钟前
Vue3 规范推荐的 <script setup> 中书写顺序(附:如何响应路由参数变化)
vue.js·路由
web打印社区2 小时前
前端实现浏览器预览打印:从原生方案到专业工具
前端·javascript·vue.js·electron
jiayong232 小时前
Vue2 与 Vue3 生态系统及工程化对比 - 面试宝典
vue.js·面试·职场和发展
徐同保2 小时前
vue.config.ts配置代理解决跨域,配置开发环境开启source-map
前端·javascript·vue.js
Hexene...2 小时前
【前端Vue】npm install时根据新的状态重新引入实际用到的包,不引入未使用到的
前端·vue.js·npm
2301_780669862 小时前
Vue(入门配置、常用指令)、Ajax、Axios
前端·vue.js·ajax·javaweb
我是ed.3 小时前
Vue3 音频标注插件 wavesurfer
前端·vue.js·音视频
Hexene...3 小时前
【前端Vue】出现elementui的index.css引入报错如何解决?
前端·javascript·vue.js·elementui
红色的小鳄鱼3 小时前
Vue 监视属性 (watch) 超全解析:Vue2 Vue3
前端·javascript·css·vue.js·前端框架·html5