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

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

1.效果图

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

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

javascript 复制代码
//给标签赋值
				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

javascript 复制代码
.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;
	}
相关推荐
游戏开发爱好者81 小时前
H5 混合应用加密 Web 资源暴露到 IPA 层防护的完整技术方案
android·前端·ios·小程序·uni-app·iphone·webview
2501_915106322 小时前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone
游戏开发爱好者82 小时前
HTTPS DDoS 排查 异常流量到抓包分析
网络协议·ios·小程序·https·uni-app·iphone·ddos
一点晖光2 小时前
小程序中web-view加载uni-app H5如何使用postMessage方法的解决方案
前端·小程序·uni-app
2501_915918413 小时前
iOS 性能监控 运行时指标与系统行为的多工具协同方案
android·macos·ios·小程序·uni-app·cocoa·iphone
qq_424409193 小时前
uniapp,通过webview内嵌h5页面,如何修改h5的大小
uni-app
00后程序员张3 小时前
IPA 混淆技术全解,从成品包结构出发的 iOS 应用安全实践与工具组合
android·安全·ios·小程序·uni-app·cocoa·iphone
郑州光合科技余经理4 小时前
定制开发实战:海外版外卖系统PHP全栈解决方案
java·服务器·开发语言·javascript·git·uni-app·php
2501_916008894 小时前
IOScer 证书到底是什么和怎么使用的完整说明
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张4 小时前
iOS 抓包工具实战指南,从代理到数据流,全流程工具分工解析
android·ios·小程序·https·uni-app·iphone·webview