最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
微信小程序实现复选框效果
时间:2022-06-25 15:25:14 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了微信小程序实现复选框片展示的具体代码,供大家参考,具体内容如下
样式部分你们自由发挥,反正这里是什么都没写的,选中和没选中直接用这个this.data.arrStatus[checkIndex]去判断就行了,之后你们都懂的。
效果预览:
js部分
// page/index/index.js
Page({
/**
* 页面的初始数据
*/
data: {
items: [
{ name: 'USA', value: '美国' },
{ name: 'CHN', value: '中国' },
{ name: 'BRA', value: '巴西' },
{ name: 'JPN', value: '日本' },
{ name: 'ENG', value: '英国' },
{ name: 'TUR', value: '法国' },
],
arr: [],
arrStatus: []
},
check: function (e) {
//获取当前选中的值
var checkValue = e.currentTarget.dataset.val;
//获取当前选中的下标
var checkIndex = e.currentTarget.dataset.index;
//当前选中的取反值
this.data.arrStatus[checkIndex] = !this.data.arrStatus[checkIndex];
if (this.data.arrStatus[checkIndex]) {
//如果当前为选中状态则将值插入进数组中
this.data.arr.push(checkValue);
}else{
//如果当前为未选中状态则将值从数组中删除并返回一个新的数组
for (var i in this.data.arr) {
if (this.data.arr[i] == checkValue) {
this.data.arr.splice(i);
}
}
}
//打印当前所选中的数据
console.log(this.data.arr);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//设置数组中每一个数据的状态
for (var i in this.data.items) {
this.data.arrStatus[i] = false;
}
},
})
wxml部分:
相关文章
- 网红张雪峰死亡为何霸榜热搜,全平台哀悼?这个人是谁?干嘛的? 03-25
- 张雪峰猝死了真的假的?网传张雪峰猝死了是怎么回事? 03-25
- 张雪峰是干嘛的?张雪峰因为什么走红?心源猝紧该怎么急救? 03-25
- 张雪峰死亡原因揭秘:急速运动心源猝死 03-25
- 张雪峰今年多大呀?怎么会突然猝死了? 03-25
- 张雪峰死了真的假的?张雪峰死亡原因揭秘 03-25
