uni-app 的 checkbox-group 复选框 最多限制两个以及多个的选项

该代码段展示了一个使用Vue.js编写的checkbox组,通过change事件处理函数`checkboxChange`限制用户可选择的项目数。当达到预设的最大选择数时,不允许再选择其他项,反之则取消所有选择。代码涉及到数组遍历、条件判断以及DOM操作。
摘要由CSDN通过智能技术生成

 

<checkbox-group @change="checkboxChange">
				<view class="select" v-for="(item,index) in items" :key="index">
					<view v-if="type=='0'">
						<checkbox style="margin-left: 40rpx;" :value="item.value" :disabled="item.checked" />
					</view>
					<view :class="type == '0'?'type2':'type1'">
						<view class="selectxibox">
							<text class="selectx1">{{item.name}}</text>
							<text
								class="selectx2">{{item.disabled.replace(/^(.{4})(?:\d+)(.{4})$/,"$1******$2")}}</text>
						</view>
						<view class="sebox">
							<image class="seboximg" src="/static/application/res.png" mode="" @click="rdelete(index)">
							</image>
						</view>
					</view>
				</view>
			</checkbox-group>
connt: 2,//需要限制的大小
				items: [{
						value: 'CHN',
						name: '中国',
						disabled: '1111111111111111111',
						checked: false
					},
					{
						value: 'USA',
						name: '美国',
						disabled: '1111111111111111111',
						checked: false
					},
					{
						value: 'UScA',
						name: '美的国',
						disabled: '1111111111111111111',
						checked: false
					},
				],
				type: ""
checkboxChange: function(e) {
				console.log(e.detail.value, 'd')

				let valueList = this.items.map(item => item.value); //将数据中的value取出  也就是e.detail.value中的值
				let arr = [] // 声明一个数组 存放 未被选中的
				valueList.forEach(item => {
					if (!e.detail.value.includes(item)) { // 将未被选中的添加到arr数组中
						arr.push(item)
					}
				})
				var items = this.items,
					values = e.detail.value;
				
				if (e.detail.value.length  == this.connt) { // 如果已选中的长度 == 限制的值
					for (let i in this.items) { // 循环全部数据
						for (let j in arr) { // 循环未被选中的数据
							if (this.items[i].value === arr[j]) {
								this.items[i].checked = true;
							}
						}
					}
				} else {
					for (let i in this.items) {
						this.items[i].checked = false;
					}
				}
			},
.type1 {
		width: 630rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 40rpx 0 0;
	}

	.type2 {
		width: 550rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.topbox2 {
		height: 100vh;
		padding: 0 40rpx;

		.addpar {
			width: 670rpx;
			height: 6vh;
			background-color: #fff;
			box-shadow: 1rpx 5rpx 15rpx #e1e1e1;
			border-radius: 20rpx;
			margin-top: 3vh;
			display: flex;
			align-items: center;
			justify-content: center;

			.addparimg {
				width: 50rpx;
				height: 50rpx;
			}

			.addpartext {
				color: #f5852f;
				margin-left: 20rpx;
			}
		}

		.select {
			width: 670rpx;
			height: 9vh;
			background-color: #fff;
			box-shadow: 1rpx 5rpx 15rpx #e1e1e1;
			margin-top: 3vh;
			border-radius: 20rpx;
			display: flex;
			align-items: center;

			.selectimg {
				width: 50rpx;
				height: 50rpx;
				margin-left: 30rpx;
			}

			.selectxibox {
				display: flex;
				flex-direction: column;
				margin-left: 40rpx;

				.selectx1 {
					font-size: 30rpx;
				}

				.selectx2 {
					margin-top: 10rpx;
					font-size: 28rpx;
					color: #b6b6b6;
				}
			}

			.sebox {
				.seboximg {
					width: 50rpx;
					height: 50rpx;
					position: relative;
					z-index: 100000;
				}
			}
		}
	}

	// 
	.topbox1 {
		height: 85vh;
		padding: 0 40rpx;

		.addpar {
			width: 670rpx;
			height: 6vh;
			background-color: #fff;
			box-shadow: 1rpx 5rpx 15rpx #e1e1e1;
			border-radius: 20rpx;
			margin-top: 3vh;
			display: flex;
			align-items: center;
			justify-content: center;

			.addparimg {
				width: 50rpx;
				height: 50rpx;
			}

			.addpartext {
				color: #f5852f;
				margin-left: 20rpx;
			}
		}

		.select {
			width: 670rpx;
			height: 9vh;
			background-color: #fff;
			box-shadow: 1rpx 5rpx 15rpx #e1e1e1;
			margin-top: 3vh;
			border-radius: 20rpx;
			display: flex;
			align-items: center;

			.selectimg {
				width: 50rpx;
				height: 50rpx;
				margin-left: 30rpx;
			}

			.selectxibox {
				display: flex;
				flex-direction: column;
				margin-left: 40rpx;

				.selectx1 {
					font-size: 30rpx;
				}

				.selectx2 {
					margin-top: 10rpx;
					font-size: 28rpx;
					color: #b6b6b6;
				}
			}

			.sebox {
				.seboximg {
					width: 50rpx;
					height: 50rpx;
					position: relative;
					z-index: 100000;
				}
			}
		}
	}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值