微信小程序文本框textarea實時提示還可以輸入多少文字


<!--pages/index29/index29.wxml-->
<page>
<view class="conts">
<textarea class="areas" placeholder='個性簽名' minlength="{{min}}" maxlength="{{max}}" bindinput="inputs">
<text class="currentWordNumber">{{currentWordNumber|0}}/{{max}}</text>
<text class="hint">{{texts}}</text>
</textarea>
</view>
</page>
// pages/index29/index29.js
Page({

/**
* 頁面的初始數據
*/
data: {
texts: "至少5個字",
min: 5,//最少字數
max: 30, //最多字數 (根據自己需求改變)
},

/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function(options) {

},

/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady: function() {

},

/**
* 生命周期函數--監聽頁面顯示
*/
onShow: function() {

},

/**
* 生命周期函數--監聽頁面隱藏
*/
onHide: function() {

},

/**
* 生命周期函數--監聽頁面卸載
*/
onUnload: function() {

},

/**
* 頁面相關事件處理函數--監聽用戶下拉動作
*/
onPullDownRefresh: function() {

},

/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function() {

},

/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function() {

},
inputs: function (e) {
// 獲取輸入框的內容
var value = e.detail.value;
// 獲取輸入框內容的長度
var len = parseInt(value.length);

//最少字數限制
if (len <= this.data.min)
this.setData({
texts: "最低五個字"
})
else if (len > this.data.min)
this.setData({
texts: " "
})

//最多字數限制
if (len > this.data.max) return;
// 當輸入框內容的長度大於最大長度限制(max)時,終止setData()的執行
this.setData({
currentWordNumber: len //當前字數
});
}

})
/* pages/index29/index29.wxss */

page {
width: 750rpx;
background: #F2F2F2;
}

page .conts {
width: 750rpx;
height: auto;
border: 1rpx soldi red;
margin-top: 30rpx;
}

page .areas {
width: 750rpx;
height: 220rpx;
background: rgba(255, 255, 255, 1);
font-size: 30rpx;
text-indent: 28rpx;
border: 1rpx solid gainsboro;
margin-top: 30rpx;
margin: 0 auto;
overflow: hidden;
position: relative;
}

page .currentWordNumber {
font-size: 28rpx;
color: gray;
position: absolute;
left: 600rpx;
top: 160rpx;
}

page .hint {
font-size: 28rpx;
position: absolute;
top: 165rpx;
left: 0rpx;
color: #f60;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM