微信小程序 textarea 统计输入长度

包修贤
2023-12-01

wxml

     <view class="weui-cells weui-cells_after-title">
          <view class="weui-cell">
               <view class="weui-cell__bd">
                    <textarea class="weui-textarea" placeholder="请输入文本" style="height: 3.3em" maxlength='100' bindinput='userInput' />
                    <view class="weui-textarea-counter">{{length}}/100</view>
               </view>
          </view>
     </view>

这里要用bindinput

js

     data: {
          ...
          length: 0
     },
     //统计输入长度
     userInput: function(e) {
          console.log("输入的内容---" + e.detail.value)
          console.log("输入的长度---" + e.detail.value.length)
          this.setData({
               length: e.detail.value.length
          })
     },

 类似资料: