uniapp h5项目实现多选按钮/多选标签/多选框

需求:实现简单多选功能,遍历数据,添加样式

1.效果图 

2.以下代码粘贴到代码中,可直接运行,html代码

<view class="page index">
				<view class="list-box">
					<view v-for="(item,index) in typeList" :key="index" @click="choice(index)"
						:class="[item.selected?'selde':'noselde']">
						{{item.selected?item.title:item.title}}
					</view>
				</view>
				<view class="valueList">
					{{selectList}}
				</view>
			</view>

3.js

//给标签赋值
				typeList: [{
						selected: false,
						title: '综合类 20积分一公斤'
					},
					{
						selected: false,
						title: '金属类 20积分一公斤'
					},
					{
						selected: false,
						title: '纸类 20积分一公斤'
					},
					{
						selected: false,
						title: '纸类 20积分一公斤'
					},
					{
						selected: false,
						title: '纸类 20积分一公斤'
					}, {
						selected: false,
						title: '纸类 20积分一公斤'
					},
					{
						selected: false,
						title: '纸类 20积分一公斤'
					}
				],
				selectId: [],

//选择按钮
			choice(index) {
				console.log('index', index)
				//当再次被选中时,取消当前选中项
				if (this.typeList[index].selected == true) {
					this.typeList[index].selected = false;
					//取消选中时删除数组中的值
					for (var i = 0; i < this.selectId.length; i++) {
						if (this.selectId[i] === this.typeList[index].title) {
							this.selectId.splice(i, 1);

						}
					}
					this.selectList = this.selectId
					console.log("选中的值", this.selectId)
				} else {
					this.typeList[index].selected = true;
					this.selectId.push(this.typeList[index].title)
					for (var i = 0; i < this.selectId.length; i++) {
						console.log("选中的值", this.selectId[i])
					}
					this.selectList = this.selectId
					console.log("选中的值", this.selectId)
				}
			}

5.css 

.list-box {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		// padding: 16rpx;
		border-radius: 10rpx;

		view {
			width: 45%;
			height: 60upx;
			line-height: 60upx;
			text-align: center;
			margin-bottom: 30upx;

			// &:not(:nth-child(3n)) {
			// 	margin-right: calc(10% / 2);
			// }
		}
	}

	/* 已选择 */
	.selde {
		border: 1px solid #3EAAFB;
		background: #3EAAFB;
		color: #FFFFFF;
		border-radius: 20rpx;
		font-size: 20rpx;
		padding: 0 10rpx;
	}

	/* 未选择 */
	.noselde {
		border: 1px solid #959595;
		background: #FFFFFF;
		color: #959595;
		border-radius: 20rpx;
		font-size: 20rpx;
		padding: 0 10rpx;
	}

	.valueList {
		margin-top: 20rpx;
		padding: 20rpx;
		display: flex;
		justify-content: center;
	}

相关推荐

  1. selenium中,怎么判断是否已

    2024-04-12 07:38:04       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 07:38:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 07:38:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 07:38:04       82 阅读
  4. Python语言-面向对象

    2024-04-12 07:38:04       91 阅读

热门阅读

  1. 用php编写网站源码的一些经验

    2024-04-12 07:38:04       41 阅读
  2. 如何在 OpenLDAP 服务器上更改账户密码

    2024-04-12 07:38:04       52 阅读
  3. ubuntu下利用ffmpeg工具将视频帧推流至rtsp

    2024-04-12 07:38:04       47 阅读
  4. 0基础刷图论最短路 2(从ATcoder 0分到1800分)

    2024-04-12 07:38:04       39 阅读
  5. Golang教程四(协程,channel,线程安全,syncMap)

    2024-04-12 07:38:04       183 阅读
  6. Pytorch 获取当前模型占用的 GPU显存的大小

    2024-04-12 07:38:04       40 阅读
  7. 动态开辟字符串malloc

    2024-04-12 07:38:04       53 阅读