uniapp textarea component exceeds maximum value

This problem is encountered in the project: the maximum input value of 50 is set for the textarea, but the input can still be continued on the Android phone, even exceeding it.

Solution: Obtain the real-time input value through the @input method. When the input value is greater than the limited number of characters n, reassign the value bound to the v-model, and assign the first n values ​​of the current input.

Additional knowledge: Since the @input event itself has parameters, when we also need to pass our own parameters, we can pass the value @input="bindTextAreaBlur2($event,'我是谁')"like this, which saves the code redundancy caused by writing two methods for two input boxes.

<view class="uni-textarea">
	<textarea v-model="query.aa" :key="query.aa" placeholder-style="color:#F76260" placeholder="占位符字体是红色的" @input="bindTextAreaBlur($event,'aa')" maxlength="20"/>
	<view>{{query.aa.length}}/20</view>
</view>
<view class="uni-textarea">
	<textarea v-model="query.bb" :key="query.bb" placeholder-style="color:#F76260" placeholder="占位符字体是红色的" @input="bindTextAreaBlur($event,'bb')" maxlength="20"/>
        <view>{{query.bb.length}}/20</view>
</view>
复制代码
data() {
    return {
        query: {
            aa: "",
            bb: ""
        }
    }
},
methods: {
    bindTextAreaBlur: function (e,c) {
        console.log(e.detail.value,c)
        let maxValue = e.detail.value
        if(maxValue.length>=5){
            this.$nextTick(()=>{
             this.query[c]=e.detail.value.substring(0,5)
            })
        }
    },
}
复制代码

Guess you like

Origin juejin.im/post/6977281427930873892