微信小程序实现工作时间段选择

本文实例为大家分享了微信小程序工作时间段选择的具体代码,供大家参考,具体内容如下

1. 效果图如上,需完成时间段的选择  以及下面的工作日选择  保存按钮为formSubmit提交后台

2.思路:

①时段用picker跟input  如果没有占位字符  则不需要input

②工作日选择用checkbox  多选的样式用label  将checkbox隐藏

③label的选择后的样式跟取消的样式,这里无需判断checked  当然也可以判断checked  我这里的方法是,先建一个放星期一到星期天的数组date,选择之后,在js里返回的detail.value里找到所选择的日期selectedList,再判断date是否在selectedList内存在(注意是date在list中是否存在)。如果存在则设对应的布尔为true,否则false。

下面为代码:

wxml

<!--pages/addtime/index.wxml-->

<view class='main'>

<view class='when'>

<form bindsubmit='formSubmit'>

<button formType='submit' id='save'>保存</button>

<!-- 选择时间段 -->

<view class="selectTime">

<text>时段:</text>

<view class="section">

<picker mode="time" value="{{time}}" start="09:00" end="21:00" bindchange="bindTimeChange" id='startTime'>

<!-- 在js中判断所选picker的id为startTime时,将value赋值给startTime,放在input内 -->

<view class="picker">

<input placeholder='开始时间' 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时,将value赋值给endTime,放在input内 -->

<view class="picker">

<input placeholder='结束时间' value='{{endTime}}' name="endTime"></input>

</view>

</picker>

</view>

</view>

<!-- 选择星期 -->

<view class='selectDay'>

<checkbox-group bindchange="checkboxChange" name="checkbox">

重复:

<!--写一个class选择后的背景色,在js中处理对应布尔值,在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({

/**

* 页面的初始数据

*/

data: {

startTime:null,

endTime: null,

selected: {"monday":false,"tuesday":false,"friday":false,"wednesday":false,"thursday":false,"sunday":false,"saturday":false},

// selected内放对应的用来判断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"];//包含所有日期的数组

var selected = this.data.selected;//先获取data中的值,好用来赋值

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){

//提交后台

console.log(e);

},

以上是 微信小程序实现工作时间段选择 的全部内容, 来源链接: utcz.com/z/358850.html

回到顶部