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

<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>

 

posted @ 2023-11-15 11:37  焦廉琨  阅读(46)  评论(0编辑  收藏  举报