首页
/
每日頭條
/
生活
/
微信小程序input輸入框的大小
微信小程序input輸入框的大小
更新时间:2026-01-27 21:28:52

在微信小程序開發中,input 用來實現文本輸入,是單行的,textarea是多行的輸入實現

1 基本使用

<textarea class="input" name="remark" placeholder="請輸入備注" auto-focus="true" />

基本效果就是顯示了一個多行的文本輸入框。

  • placeholder 輸入框為空時的占位符
  • auto-focus 自動聚集,拉起鍵盤,需要注意的是一個頁面中隻能有一個 input 标簽 或者 textarea 來設置這個屬性

微信小程序input輸入框的大小(微信小程序文本輸入)1

我在這裡為明顯效果所以設置了個邊框樣式

.input{ /* 邊框 */ border:1px solid red; padding: 10rpx; }

2 獲取輸入框中的内容

bindinput 屬性用來綁定鍵盤輸入時的事件監聽,也就是可以實時獲取輸入中的内容 。

當然 在你的處理函數中可以直接 return 一個結果來替換輸入框中的内容。

<textarea class="input" name="remark" placeholder="請輸入備注" bindinput="remarkInputAction" />

對應的 js

/** * 輸入框實時回調 * @param {*} options */ remarkInputAction: function (options) { //獲取輸入框輸入的内容 let value = options.detail.value; console.log("輸入框輸入的内容是 " value) },

效果

微信小程序input輸入框的大小(微信小程序文本輸入)2

3 輸入框焦點監聽

應用場景還是比較多的,比如輸入結束時 去校驗個數據什麼的

  • bindfocus 輸入框獲取到輸入焦點時
  • bindblur 輸入框焦點移出
  • bindconfirm 點擊鍵盤的回車鍵或者是完成按鈕時回調的事件

<textarea class="input" name="remark" placeholder="請輸入備注" bindfocus="remarkFocusAction" bindblur="remarkBlurAction" bindconfirm="remarkConfirm" />

對應的 js

remarkFocusAction: function (options) { //輸入框焦點獲取 let value = options.detail.value; console.log("輸入框焦點獲取 " value) }, remarkBlurAction: function (options) { //輸入框焦點移出 let value = options.detail.value; console.log("輸入框焦點移出 " value) }, remarkConfirm: function (options) { //點擊了鍵盤上的完成按鈕 let value = options.detail.value; console.log("點擊了鍵盤上的完成按鈕 " value) },

效果圖

微信小程序input輸入框的大小(微信小程序文本輸入)3

4 auto-height 自動增高與獲取行數
  • auto-height 默認為false, 為true時,自動增高,默認顯示一行,為true時 style.height設置不生效
  • bindlinechange 換行時會觸發

<textarea auto-height="true" bindlinechange="remarkLineAction" />

remarkLineAction: function (options) { //行數 let lineCount = options.detail.lineCount; let height = options.detail.height; let heightRpx = options.detail.heightRpx; console.log("輸入框行數變化 " lineCount) },

微信小程序input輸入框的大小(微信小程序文本輸入)4

5 maxlength 限制輸入的文本長度,默認是 140字符,配置為 -1 時代表無限制

<textarea maxlength="1" />

6 使用實例

微信小程序input輸入框的大小(微信小程序文本輸入)5

<view class="inputshow"> <textarea maxlength='500' placeholder-style="color:#5F5F5F;" bindinput='limitWord' value="{{content}}" placeholder='請輸入備注(最多500個字)'></textarea> <view class="clear"> <text style="float: right">{{currentWord}}/{{maxWord}}(最多可輸入500字)</text> </view> </view>

Page({ /** * 頁面的初始數據 */ data: { //字數限制 maxWord: 500, currentWord: 0 }, limitWord:function(e){ var that = this; var value = e.detail.value; //解析字符串長度轉換成整數。 var wordLength = parseInt(value.length); if (that.data.maxWord < wordLength) { return ; } that.setData({ currentWord: wordLength }); }, );

.inputshow{ padding: 10px; background-color: white; border:1px solid red; padding: 10rpx; }


完畢

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
黃花梨家具盤玩後的效果圖
黃花梨家具盤玩後的效果圖
孤品是指獨一無二的的物品,具有極大的稀缺性,收藏愛好者們經常提及,是大家追逐的目标。在明清黃花梨傳世家具中,堪稱孤品的很少。清黃花梨豎棂格箱籠,四面透風的棂格結構,讓人想起過去存放食物的碗櫥,偷腥的貓兒可望而不可即,俗稱"氣死貓&#...
2026-01-27
哺乳期不能吃韭菜嗎
哺乳期不能吃韭菜嗎
大家好,我是小浩浩媽!今天來說下寶媽在哺乳期的時候,你認為不能做的有哪些。目前我也在哺乳期,可是我的一個朋友跟我說,哺乳期很多東西不能吃,很多事情不能做,你要記住了啊。她就是那種傳統的觀念,比如辣的不能吃,冰的不能吃,酸的也要少吃,不能化妝...
2026-01-27
誤會男友的愛情故事
誤會男友的愛情故事
本故事已由作者:黴蛋非蛋,授權每天讀點故事app獨家發布,旗下關聯賬号“每天讀點故事”獲得合法轉授權發布,侵權必究。1鹿悄從辦公室出來,心驚膽戰地掏出手機在一個叫“此處禁言”的微信群裡發信息。小鹿悄悄(翻譯):組長對我笑了,孩子害怕。兩隻老...
2026-01-27
尿結石最快的排出方法
尿結石最快的排出方法
泌尿系結石包括腎結石、輸尿管結石、膀胱結石和尿道結石。其中腎結石和輸尿管結石為多見。我們知道腎髒是分泌尿液的器官,同樣腎髒也是産生結石的器官。由于遺傳因素、飲水因素、地域因素、生活習慣等各方面原因,尿液産生結晶,日久形成結石。結石下落到輸尿...
2026-01-27
劉半仙解說開局一把鐵劍第二季
劉半仙解說開局一把鐵劍第二季
劉半仙解說開局一把鐵劍第二季?仙仙你好:,今天小編就來聊一聊關于劉半仙解說開局一把鐵劍第二季?接下來我們就一起去研究一下吧!劉半仙解說開局一把鐵劍第二季仙仙你好:當你看到這篇文章時,一定會大吃一驚,也許在你的記憶中不會對我有很深的印象,因為...
2026-01-27
Copyright 2023-2026 - www.tftnews.com All Rights Reserved