textarea监视内部字数并限制字数

发布时间 2023-11-15 11:45:42作者: 焦廉琨
<div class="textBox">
        <span class="span1">内容:</span>
        <textarea name="" id="" cols="30" rows="10" placeholder="请输入/粘贴文本" v-model="inputText"
          @input="handleInput"></textarea>
        <span class="span2">字数统计{{ charCount }}/{{ limit }}</span>
</div>
<script>
import { ref, watch } from 'vue';
const inputText = ref('');
const charCount = ref(0);
const limit = 4000;
const handleInput = (event) => {
  inputText.value = event.target.value.slice(0, limit);
};

watch(inputText, (newValue) => {
  charCount.value = newValue.length;
});
</script>