首先呢,先附上代碼,先看看代碼。
取值:
1 <view class='sameBox'> 2 <view> 3 <text>*</text>會員賬號: 4 </view> 5 <input type="text" bindinput="memberAccountInput" name="memberAccount" value='{{accountVal}}' placeholder="請輸入您的賬號" /> 6 </view>
1 memberAccountInput(e) { 2 this.setData({ 3 memberAccount: e.detail.value 4 }) 5 },
在此呢,有的可能還沒明白,說話的取值呢?到底在哪里,別着急。
到這里呢,我們要取這個input的值了,就這句話就OK了,是的,拿的就是name屬性的值。--- console.log(this.data.memberAccount)
賦值:
在data中設置一個名為accountVal的變量
data:{ accountVal: '', }
然后呢,賦值即可(加粗部分)
app.wxRequest('POST', app.globalData.URL + '/aaa', 0, (res) => { console.log(res) if(res.status == 1){ this.setData({ accountVal : res.data }) }else{ wx.showToast({ title: res.msg, icon: 'none' }) } console.log(this.data.memberAccount) }, (err) => { console.log(err.errMsg) });
上面的代碼呢,bindinput是自帶的事件方法(鍵盤輸入時觸發,event.detail = {value, cursor, keyCode},keyCode 為鍵值,2.1.0 起支持,處理函數可以直接 return 一個字符串,將替換輸入框的內容),
詳細了解官方介紹:https://developers.weixin.qq.com/miniprogram/dev/component/input.html,
bindinput事件呢跟name屬性組合使用,上面的代碼呢大家也看了,也標有顏色區分。
紅色的為input的取值方法,name呢是跟bindinput 事件組合使用,一便在事件觸發時使用。
藍色的就是賦值了,我們在做項目時候呢肯定需要后台的數據的,那么此時就需要賦值了,小程序的賦值呢,跟平常的使用差不多,定義變量,賦值變量。
到此呢,希望對大家有用吧。