微信小程序?qū)崿F(xiàn)工作時(shí)間段選擇
本文實(shí)例為大家分享了微信小程序工作時(shí)間段選擇的具體代碼,供大家參考,具體內(nèi)容如下

1. 效果圖如上,需完成時(shí)間段的選擇 以及下面的工作日選擇 保存按鈕為formSubmit提交后臺(tái)
2.思路:
①時(shí)段用picker跟input 如果沒(méi)有占位字符 則不需要input
②工作日選擇用checkbox 多選的樣式用label 將checkbox隱藏
③label的選擇后的樣式跟取消的樣式,這里無(wú)需判斷checked 當(dāng)然也可以判斷checked 我這里的方法是,先建一個(gè)放星期一到星期天的數(shù)組date,選擇之后,在js里返回的detail.value里找到所選擇的日期selectedList,再判斷date是否在selectedList內(nèi)存在(注意是date在list中是否存在)。如果存在則設(shè)對(duì)應(yīng)的布爾為true,否則false。
下面為代碼:
wxml
<!--pages/addtime/index.wxml-->
<view class='main'>
<view class='when'>
<form bindsubmit='formSubmit'>
<button formType='submit' id='save'>保存</button>
<!-- 選擇時(shí)間段 -->
<view class="selectTime">
<text>時(shí)段:</text>
<view class="section">
<picker mode="time" value="{{time}}" start="09:00" end="21:00" bindchange="bindTimeChange" id='startTime'>
<!-- 在js中判斷所選picker的id為startTime時(shí),將value賦值給startTime,放在input內(nèi) -->
<view class="picker">
<input placeholder='開(kāi)始時(shí)間' value='{{startTime}}' name="startTime"></input>
</view>
</picker>
</view>
<text>至</text>
<view class="section">
<picker mode="time" value="{{time}}" start="09:00" end="21:00" bindchange="bindTimeChange" id='endTime'>
<!-- 在js中判斷所選picker的id為endTime時(shí),將value賦值給endTime,放在input內(nèi) -->
<view class="picker">
<input placeholder='結(jié)束時(shí)間' value='{{endTime}}' name="endTime"></input>
</view>
</picker>
</view>
</view>
<!-- 選擇星期 -->
<view class='selectDay'>
<checkbox-group bindchange="checkboxChange" name="checkbox">
重復(fù):
<!--寫一個(gè)class選擇后的背景色,在js中處理對(duì)應(yīng)布爾值,在class中用三元式判斷布爾并加背景色 -->
<label class="checkbox {{selected.monday?'selectedColor':''}}" for='monday'>
一
<checkbox value="monday" id='monday' hidden/>
</label>
<label class="checkbox {{selected.tuesday?'selectedColor':''}}" for='tuesday'>
二
<checkbox value="tuesday" id='tuesday' hidden/>
</label>
<label class="checkbox {{selected.wednesday?'selectedColor':''}}" for='wednesday'>
三
<checkbox value="wednesday" id='wednesday' hidden/>
</label>
<label class="checkbox {{selected.thursday?'selectedColor':''}}" for='thursday'>
四
<checkbox value="thursday" id='thursday' hidden/>
</label>
<label class="checkbox {{selected.friday?'selectedColor':''}}" for='friday'>
五
<checkbox value="friday" id='friday' hidden/>
</label>
<label class="checkbox {{selected.saturday?'selectedColor':''}}" for='saturday'>
六
<checkbox value="saturday" id='saturday' hidden/>
</label>
<label class="checkbox {{selected.sunday?'selectedColor':''}}" for='sunday'>
七
<checkbox value="sunday" id='sunday' hidden/>
</label>
</checkbox-group>
</view>
</form>
</view>
</view>
wx js
// pages/mine/index.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
startTime:null,
endTime: null,
selected: {"monday":false,"tuesday":false,"friday":false,"wednesday":false,"thursday":false,"sunday":false,"saturday":false},
// selected內(nèi)放對(duì)應(yīng)的用來(lái)判斷class的布爾json
},
bindTimeChange: function(e){
if (e.currentTarget.id=="startTime") {
this.setData({startTime:e.detail.value});
}else if (e.currentTarget.id=="endTime") {
this.setData({endTime:e.detail.value});
}
},
checkboxChange: function(e){
var selectedList = e.detail.value;
var date = ["monday","tuesday","friday","wednesday","thursday","sunday","saturday"];//包含所有日期的數(shù)組
var selected = this.data.selected;//先獲取data中的值,好用來(lái)賦值
for (var i = 0; i < date.length; i++) {
if (selectedList.indexOf(date[i])!=-1) { //判斷所有的日期date在所選擇的列表中是否存在,存在則給class
selected[date[i]] = true;
}else{
selected[date[i]] = false;
}
}
this.setData({selected:selected});
},
formSubmit:function(e){
//提交后臺(tái)
console.log(e);
},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript的9種繼承實(shí)現(xiàn)方式歸納
這篇文章主要介紹了JavaScript的9種繼承實(shí)現(xiàn)方式歸納,本文講解了原型鏈繼承、原型繼承(非原型鏈)、臨時(shí)構(gòu)造器繼承、屬性拷貝、對(duì)象間繼承等繼承方式,需要的朋友可以參考下2015-05-05
微信JSSDK實(shí)現(xiàn)打開(kāi)攝像頭拍照再將相片保存到服務(wù)器
這篇文章主要介紹了微信JSSDK實(shí)現(xiàn)打開(kāi)攝像頭拍照再將相片保存到服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
總結(jié)分享10 個(gè)超棒的 JavaScript 簡(jiǎn)寫技巧
這篇文章主要總結(jié)分享10 個(gè)超棒的 JavaScript 簡(jiǎn)寫技巧,有合并數(shù)組、克隆數(shù)組、解構(gòu)賦值、模板字面量等技巧,需要的朋友可以參考一下2022-06-06
JavaScript使用HTML5的window.postMessage實(shí)現(xiàn)跨域通信例子
這篇文章主要介紹了JavaScript使用HTML5的window.postMessage實(shí)現(xiàn)跨域通信例子,需要的朋友可以參考下2014-04-04
JavaScript模擬實(shí)現(xiàn)新浪下拉菜單效果
這篇文章主要為大家介紹了如何通過(guò)JavaScript模擬實(shí)現(xiàn)新浪的下拉菜單效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-03-03
JavaScript利用canvas實(shí)現(xiàn)鼠標(biāo)跟隨特效
canvas是一個(gè)很神奇的玩意兒,比如畫表格、畫海報(bào)圖都要用canvas去做。本文就來(lái)利用canvas制作個(gè)簡(jiǎn)單的鼠標(biāo)跟隨特效,快跟隨小編一起學(xué)習(xí)一下吧2022-10-10

