微信小程序 checkbox使用實例解析
這篇文章主要介紹了微信小程序 checkbox使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
效果圖如下:
實例代碼如下:
type_add.js
// pages/detail_add/detail_add.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { selectData: "", //下拉列表的數(shù)據(jù) height: 20, focus: false }, checkboxChange: function(e) { console.log('checkbox發(fā)生change事件,攜帶value值為:', e.detail.value) console.log("長度:" + e.detail.value.length); this.setData({ typeId: e.detail.value, length: e.detail.value.length }) }, formSubmit: function(e) { console.log('form發(fā)生了submit事件,攜帶數(shù)據(jù)為:' + e.detail.value.amount + ", " + e.detail.value.typeId + ", " + this.data.remark + ", " + this.data.date + ", " + this.data.time); var amount = e.detail.value.amount; var typeId = this.data.typeId; var date = this.data.date; var time = this.data.time; var remark = e.detail.value.remark; var createDate = date + " " + time; var length = this.data.length; console.log("length:" + length); console.log("date:" + date); console.log("time:" + time); console.log("createDate:" + createDate) if (amount == null || amount == "") { wx.showToast({ title: "支出金額不能為空", icon: 'none', duration: 1500 }) } else if (typeId == null || typeId == "") { wx.showToast({ title: "支出類型不能為空", icon: 'none', duration: 1500 }) } else if (length >= 2) { wx.showToast({ title: "支出類型只能選擇一種", icon: 'none', duration: 1500 }) } else if (date == null || date == "") { wx.showToast({ title: "日期不能為空", icon: 'none', duration: 1500 }) } else if (time == null || time == "") { wx.showToast({ title: "時間不能為空", icon: 'none', duration: 1500 }) } else if (remark == null || remark == "") { wx.showToast({ title: "備注不能為空", icon: 'none', duration: 1500 }) } else { wx.request({ url: getApp().globalData.urlPath + "spendingDetail/add", method: "POST", data: { amount: amount, typeId: typeId, createDate: createDate, remark: remark }, header: { "Content-Type": "application/x-www-form-urlencoded" }, success: function(res) { console.log(res.data.code); if (res.statusCode == 200) { //訪問正常 if (res.data.code == "000000") { wx.showToast({ title: "添加支出詳情成功", icon: 'success', duration: 3000, success: function() { wx.navigateTo({ url: '../detail/detail' }) } }) } } else { wx.showLoading({ title: '系統(tǒng)異常', fail }) setTimeout(function() { wx.hideLoading() }, 2000) } } }) } }, formReset: function() { console.log('form發(fā)生了reset事件') }, bindDateChange: function(e) { console.log('picker發(fā)送選擇改變,攜帶值為', e.detail.value) this.setData({ date: e.detail.value }) }, bindTimeChange: function(e) { console.log('picker發(fā)送選擇改變,攜帶值為', e.detail.value) this.setData({ time: e.detail.value }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function(options) { wx.setNavigationBarTitle({ title: "添加支出詳情" }) var userCode = wx.getStorageSync('userId').toString(); var self = this; wx.request({ url: getApp().globalData.urlPath + "spendingType/types", //json數(shù)據(jù)地址 data: { userCode: userCode }, headers: { "Content-Type": "application/x-www-form-urlencoded" }, success: function(res) { console.log("res.data.data.typeName:" + res.data.data) self.setData({ selectData: res.data.data }) } }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function() { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function() { }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function() { }, /** * 生命周期函數(shù)--監(jiān)聽頁面卸載 */ onUnload: function() { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作 */ onPullDownRefresh: function() { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function() { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function() { } })
type_add.wxml:
<form bindsubmit="formSubmit" bindreset="formReset"> <view class="section"> <text>支出金額</text> <input name="input" name="amount" placeholder="請輸入支出金額" /> </view> <view class="section"> <text>支出類型</text> <checkbox-group bindchange="checkboxChange"> <label class="checkbox" wx:for="{{selectData}}"> <checkbox value="{{item.typeId}}" checked="{{item.checked}}" />{{item.typeName}} </label> </checkbox-group> </view> <view> <text>創(chuàng)建時間</text> <view class="section"> <picker mode="date" value="{{date}}" start="2018-09-01" end="2030-09-01" bindchange="bindDateChange"> <view class="picker"> 選擇日期: {{date}} </view> </picker> </view> <view class="section"> <picker mode="time" value="{{time}}" start="00:00=" end="23:59" bindchange="bindTimeChange"> <view class="picker"> 選擇時間: {{time}} </view> </picker> </view> </view> <view class="section"> <text>備注</text> <input name="input" name="remark" placeholder="請輸入備注" /> </view> <view> <text>\n</text> </view> <view class="btn-area"> <button form-type="submit">提交</button> <view> <text>\n</text> </view> <button form-type="reset">重置</button> </view> </form>
bindchange=”checkboxChange” 相當(dāng)于js中的onchange事件。
上述中的form表單基本就是參考官方文檔改的。
有一段代碼還是要提一下:
self.setData({ selectData: res.data.data })
self其實相當(dāng)于this,意為當(dāng)前。每次觸發(fā)事件,對應(yīng)的值都會進行存儲,用于與后臺通信進行數(shù)組傳遞,
type_add.wxss:
/* pages/login/login.wxss */ form{ width: 310px; height: 240px; line-height: 40px; /* border: 1px solid red; */ } input{ border: 1px solid #ccc; width: 310px; height: 40px; } .button{ margin-top: 20px; } .header text{ font-size: 25px; color: #666; } form text{ font-size: 20px; color: #666; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ng-options和ng-checked在表單中的高級運用(推薦)
AngularJS是當(dāng)前非常的流行的前端框架,它的語法糖非常多,也極大的方便了前端開發(fā)者。這篇文章主要介紹了ng-options和ng-checked在表單中的高級運用,需要的朋友可以參考下2017-01-01JS Promise axios 請求結(jié)果后面的.then() 是什么意思
本文主要介紹了JS Promise axios 請求結(jié)果后面的 .then() 是什么意思,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01uniapp發(fā)送formdata表單請求2種方法(全網(wǎng)最簡單方法)
這篇文章主要給大家介紹了關(guān)于uniapp發(fā)送formdata表單請求2種方法的相關(guān)資料,本文介紹的方法應(yīng)該是全網(wǎng)最簡單方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-09-09javascript自動生成包含數(shù)字與字符的隨機字符串
這篇文章主要介紹了javascript自動生成包含數(shù)字與字符的隨機字符串,涉及Math.random()和Math.floor()兩個函數(shù)的使用技巧,需要的朋友可以參考下2015-02-02JavaScript測試工具之Karma-Jasmine的安裝和使用詳解
Jasmine是一個Javascript的測試工具,在Karma上運行Jasmine可完成Javascript的自動化測試、生成覆蓋率報告等。本文不包含Jasmine的使用細節(jié),這幾天我會寫一篇Jasmine的入門文章,有興趣的朋友到時候可以看一下2015-12-12