作者:JoyJin,來自原文地址
附上微信小程序開發(fā)文檔鏈接:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/MINA.html
form表單:
當(dāng)點(diǎn)擊 <form/> 表單中 formType 為 submit 的 <button/> 組件時,會將表單組件中的 value 值進(jìn)行提交,需要在表單組件中加上 name 來作為 key。
bindtap 用戶點(diǎn)擊時觸發(fā)
bindchange 用戶輸入完成時觸發(fā)(建議要輸入中文的input采用這個點(diǎn)擊事件)
判斷兩次密碼不一致用 !== 相比較 例如:if(that.data.password !== that.data.password_confirmation){ }
-
<form bindsubmit="loginTap">
-
<view class="section">
-
<input placeholder="輸入手機(jī)號" maxlength="11" placeholder-style="color:#fff" name="phone" bindtap="phone"/> //placeholder-style 設(shè)置樣式
-
</view> //hover-class="none" 設(shè)置按鈕按下的樣式及狀態(tài)
-
<button hover-class="other-button-hover" form-type="submit" bindtap="phoneBtn"> 登錄 </button>
-
</form>
-
Page({
-
data: loginData,
-
loginTap: function (e) {
-
var that = this //這句很重要
-
var loginData = e.detail.value //獲取表單里所有key的值
-
wx.request({
-
method: 'POST',
-
url: 'https://....', //小程序只能采用https
-
data: loginData, //請求的數(shù)據(jù)
-
header: {'content-type': 'application/json'},
-
success: function (res) {
-
var tokend = res.data.token; //獲取后臺token
-
wx.setStorageSync('tokend', tokend) //存儲token
-
if (res.code == 200) {
-
wx.switchTab({ //跳轉(zhuǎn)到 tabBar 頁面,并關(guān)閉其他所有非 tabBar 頁面,路徑后不能帶參數(shù)(需在 app.json 的 tabBar 字段定義的頁面)
-
url: '../index/index',
-
})
-
}if (res.code == 400) {
-
wx.showToast({ //消息提示框,默認(rèn)只能success,loading兩種形式,可引進(jìn)圖片方式
-
title: '手機(jī)號碼不正確',
-
image: '../Image/error.png',
-
duration: 2000
-
})
-
}
-
},
-
})
-
}
-
})
也可以就單獨(dú)獲取每個input的值
-
Page({
-
data:{
-
phone:''
-
},
-
phone:function(e){ //獲取input值
-
var that = this;
-
that.setData({
-
phone: e.detail.value
-
})
-
},
-
phoneBtn: function (e) {
-
var that = this;
-
wx.request({
-
url: 'https://....',
-
method: 'GET',
-
header: { 'content-type': 'application/json' },
-
data: {
-
'phone': that.data.phone //請求的數(shù)據(jù)
-
},
-
success: function (res) {}
-
})
-
},
-
})