微信小程序 input輸入及動態(tài)設(shè)置按鈕的實(shí)現(xiàn)
微信小程序 input輸入及動態(tài)設(shè)置按鈕的實(shí)現(xiàn)
【需求】實(shí)現(xiàn)當(dāng)手機(jī)號已填寫和協(xié)議已勾選時(shí),“立即登錄”按鈕變亮,按鈕可點(diǎn)擊;若有一個(gè)不滿足,按鈕置灰,不可點(diǎn)擊;實(shí)現(xiàn)獲取短信驗(yàn)證碼,倒計(jì)時(shí)提示操作;對不滿足要求內(nèi)容進(jìn)行toast彈窗提示。
<view class="container">
<!--手機(jī)號-->
<view class="section">
<text class="txt">手機(jī)號</text>
<input value="{{mobile}}" placeholder-class="placeholder" placeholder="11位手機(jī)號碼" type="number" maxlength="11"
bindinput="mobileInput"/>
</view>
<!--圖片驗(yàn)證碼-->
<view class="section">
<view>
<text class="txt">圖形驗(yàn)證碼</text>
<input placeholder-class="placeholder" placeholder="輸入圖形驗(yàn)證碼" type="text" maxlength="4"
bindinput="imgCaptchaInput"/>
</view>
<image class="imgBtn" src="{{imgCodeSrc}}" bindtap="getImgCode"></image>
</view>
<!--短信驗(yàn)證碼-->
<view class="section">
<view>
<text class="txt">驗(yàn)證碼</text>
<input placeholder-class="placeholder" placeholder="輸入驗(yàn)證碼" type="number" maxlength="6"
bindinput="smsCaptchaInput"/>
</view>
<view class="smsBtn" bindtap="getSMS">{{captchaText}}</view>
</view>
<view class="agree" style="margin-top:40rpx">
<checkbox-group bindchange="checkboxChange">
<checkbox class="check" value="1" checked="true" bindchange="checkboxChange"></checkbox>
</checkbox-group>
<span>已閱讀并同意</span> <text style="color:#98c7ff" bindtap="xieyi">《用戶使用協(xié)議》</text>
</view>
<view class="regist {{phoneAll&&checkAgree?'active':''}}" bindtap="regist">立即登錄</view>
</view>
<!--mask-->
<view class="toast_mask" wx:if="{{isShowToast}}"></view>
<!--以下為toast顯示的內(nèi)容-->
<view class="toast_content_box" wx:if="{{isShowToast}}">
<view class="toast_content">
<view class="toast_content_text">
{{toastText}}
</view>
</view>
</view>
js
// 獲取全局應(yīng)用程序?qū)嵗龑ο?
const app = getApp()
Page({
data: {
//toast默認(rèn)不顯示
isShowToast: false,
mobile: '',
imgCode: '',
code: '',
// inviteCode: '',
errorContent: '請輸入手機(jī)號',
timer: 60,
captchaText: '獲取驗(yàn)證碼',
captchaSended: false,
isReadOnly: false,
capKey: '',
sendRegist: false,
imgCodeSrc: '',
phoneAll: false,
checkAgree:true,
checkboxValue:[1],
},
// 顯示彈窗
showToast(txt, duration = 1500) {
//設(shè)置toast時(shí)間,toast內(nèi)容
this.setData({
count: duration,
toastText: txt
});
var _this = this;
// toast時(shí)間
_this.data.count = parseInt(_this.data.count) ? parseInt(_this.data.count) : 3000;
// 顯示toast
_this.setData({
isShowToast: true,
});
// 定時(shí)器關(guān)閉
setTimeout(function () {
_this.setData({
isShowToast: false
});
}, _this.data.count);
},
// 雙向綁定mobile
mobileInput(e) {
this.setData({
mobile: e.detail.value
});
if(this.data.mobile.length===11){
this.setData({
phoneAll: true
});
}else if(this.data.mobile.length<11){
this.setData({
phoneAll: false
});
}
},
// 雙向綁定img驗(yàn)證碼
imgCaptchaInput(e) {
this.setData({
imgCode: e.detail.value
});
},
// 雙向綁定sms驗(yàn)證碼
smsCaptchaInput(e) {
this.setData({
code: e.detail.value
});
},
// 同意協(xié)議
checkboxChange(e) {
this.data.checkboxValue = e.detail.value;
if(this.data.checkboxValue[0]==1){
this.setData({
checkAgree: true
});
}else {
this.setData({
checkAgree: false
});
}
},
// 獲取短信驗(yàn)證碼
getSMS() {
var that = this.data;
if (!that.mobile) {
this.showToast('請輸入手機(jī)號');
} else if (that.mobile.length != 11 || isNaN(that.mobile)) {
this.showToast('請輸入正確手機(jī)號');
}
else if (that.imgCode.length != 4) {
this.showToast('請輸入正確圖片驗(yàn)證碼');
}
else {
if (that.captchaSended) return;
this.setData({
captchaSended: true
})
app.api.getSMSByMobileAndCaptcha({
mobile: that.mobile,
capKey: that.capKey,
code: that.imgCode,
type:1
}).then((result) => {
this.showToast(result.message);
if (result.code != 1) {
this.getImgCode();
this.setData({
captchaSended: false,
});
} else {
var counter = setInterval(() => {
that.timer--;
this.setData({
timer: that.timer,
captchaText: `${that.timer}秒`,
isReadOnly: true
});
if (that.timer === 0) {
clearInterval(counter);
that.captchaSended = false;
that.captchaText = '獲取驗(yàn)證碼';
this.setData({
timer: 60,
captchaText: '獲取驗(yàn)證碼',
captchaSended: false
})
}
}, 1000);
}
});
}
},
// 獲取圖形碼
getImgCode() {
var capKey = "zdx-weixin" + Math.random();
this.setData({
imgCodeSrc: "http://prezdx.geinihua.com/invite/WeChat/verify?capKey=" + capKey,
capKey: capKey
});
},
//用戶使用協(xié)議
xieyi() {
wx.navigateTo({
url: '../userXieyi/userXieyi'
})
},
// 注冊
regist() {
var that = this.data;
if(!that.checkAgree||!that.phoneAll){
return
}
// sessionCheck為1,目的是防止微信code碼先于session過期
var code = wx.getStorageSync('wxCode');
var sessionCheck = wx.getStorageSync('sessionCheck');
wx.setStorageSync('mobile',that.mobile);
if (!that.mobile) {
this.showToast('請輸入手機(jī)號');
} else if (that.mobile.length != 11 || isNaN(that.mobile)) {
this.showToast('請輸入正確手機(jī)號');
} else if (that.code.length != 6) {
this.showToast('請輸入正確驗(yàn)證碼');
} else {
wx.showLoading({
title: '加載中...',
});
app.api.loginByCaptcha({
mobile: that.mobile,
smsCode: that.code,
code: code,
sessionCheck:sessionCheck,
}).then((res) => {
wx.hideLoading();
if (res.code == 2||res.code==1) {
//注冊成功
wx.setStorageSync('token', res.businessObj.token);
wx.setStorageSync('userId',res.businessObj.userId);
this.sucessCb(res);
app.globalData.isLogin = true; //設(shè)置為登錄成功
} else {
this.showToast(res.message);
}
});
}
},
// 成功回調(diào)
sucessCb(res) {
wx.redirectTo({
url: '/pages/index/index'
})
},
onLoad: function () {
this.getImgCode();
var that=this;
if(wx.getStorageSync('mobile')){
that.setData({
mobile: wx.getStorageSync('mobile'),
})
}
if(this.data.mobile.length===11){
this.setData({
phoneAll: true
});
}
},
})
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 解決uni-app微信小程序input輸入框在底部時(shí),鍵盤彈起頁面整體上移問題
- 一文解決微信小程序button、input和image表單組件
- 微信小程序input、textarea層級過高穿透的問題解決
- 微信小程序點(diǎn)擊按鈕動態(tài)切換input的disabled禁用/啟用狀態(tài)功能
- 微信小程序事件 bindtap bindinput代碼實(shí)例
- 微信小程序?qū)崿F(xiàn)提交input信息到后臺的方法示例
- 詳解微信小程序input標(biāo)簽正則初體驗(yàn)
- 微信小程序input框中加入小圖標(biāo)的實(shí)現(xiàn)方法
- 微信小程序使用input組件實(shí)現(xiàn)密碼框功能【附源碼下載】
- 微信小程序中input標(biāo)簽詳解及簡單實(shí)例
- 微信小程序中input組件為什么設(shè)置readonly只讀屬性沒有作用(失效)?
相關(guān)文章
Servlet3.0與純javascript通過Ajax交互的實(shí)例詳解
Servlet與純javascript通過Ajax交互,對于很多人來說應(yīng)該很簡單。不過還是寫寫,方便Ajax學(xué)習(xí)的后來者2018-03-03
詳解微信小程序 通過控制CSS實(shí)現(xiàn)view隱藏與顯示
這篇文章主要介紹了微信小程序 通過控制CSS實(shí)現(xiàn)view隱藏與顯示的相關(guān)資料,需要的朋友可以參考下2017-05-05
JavaScript選擇器函數(shù)querySelector和querySelectorAll
這篇文章主要介紹了?JavaScript選擇器函數(shù)querySelector和querySelectorAll,下面文章圍繞querySelector和querySelectorAll的相關(guān)資料展開詳細(xì)內(nèi)容,需要的朋友可以參考一下2021-11-11
跨端開發(fā)框架avm組件封裝經(jīng)驗(yàn)分享
這篇文章主要為大家介紹了跨端開發(fā)框架avm組件封裝的經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
微信小程序 省市區(qū)選擇器實(shí)例詳解(附源碼下載)
這篇文章主要介紹了微信小程序 省市區(qū)選擇器實(shí)例詳解的相關(guān)資料,區(qū)域間手勢滑動切換,標(biāo)題欄高亮隨之切換,反之亦然;當(dāng)前選中標(biāo)題紅色高亮;回到前一級點(diǎn)擊某區(qū)域后,清空子代的區(qū)域,需要的朋友可以參考下2017-01-01
詳解微信小程序如何實(shí)現(xiàn)類似ChatGPT的流式傳輸
這篇文章主要為大家介紹了微信小程序如何實(shí)現(xiàn)類似ChatGPT的流式傳輸示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

