indexof实现多选

<template>
	<div class="biaoqian">
				<button v-for="(item,index) in biaoqianList" 
				:key='index' 
				class="btn" 
				type="default" 
				size="mini"
				:class="{'active': isChange.indexOf(index)!=-1}" 
				@click="clickBtn(index)">{{item}}</button>
		</div>
</template>
export default{
		data(){
			return{
			    isChange:[],  //多选
				biaoqianList:['早餐','午餐','晚餐','宵夜'],
				foodChose:[]
			  }
			},
		methods:{
				clickBtn(index){
					// 多选
					if (this.isChange.indexOf(index) == -1) {
						if(this.isChange.length == 4){
							uni.showToast({
								title:'最多选择四项',
								icon:'none'
							})
						}else{
							this.isChange.push(index);//选中添加到数组里
						}
					} else {
						this.isChange.splice(this.isChange.indexOf(index), 1); //取消选中
					}
					console.log(this.isChange)
				},
		}
}

<style lang="less">
	.biaoqian{
				display: flex;
				justify-content: start;
				align-items: center;
				.active{
					background-color: #76d2f4 ;
					color: white;	
				}
				.btn{
				border:0.01px solid #76d2f4;
				background-color:white ;
				color: #76d2f4;
			}
		}
</style>

在这里插入图片描述

el-autocomplete是Element UI库中的一个组件,用于实现输入框自动完成功能。如果要实现多选,在el-autocomplete组件中需要添加一个多选的标志,并且使用v-model绑定一个数组来存储选择的值。 具体实现步骤如下: 1. 在el-autocomplete组件上添加multiple属性,表示开启多选模式。例如: ``` <el-autocomplete v-model="selectedItems" :fetch-suggestions="querySearch" placeholder="请输入内容" :multiple="true" > </el-autocomplete> ``` 2. 定义一个数组selectedItems,用于存储所选中的值。例如: ``` data() { return { selectedItems: [] } } ``` 3. 在querySearch方法中,返回一个数组,用于显示下拉列表中的选项内容。例如: ``` querySearch(queryString, cb) { const items = [ { value: '选项1' }, { value: '选项2' }, { value: '选项3' }, { value: '选项4' }, { value: '选项5' } ]; const results = queryString ? items.filter(this.createFilter(queryString)) : items; cb(results); }, ``` 4. 在createFilter方法中,定义一个过滤器,用于过滤出符合条件的选项。例如: ``` createFilter(queryString) { return (item) => { return item.value.indexOf(queryString) !== -1; }; }, ``` 5. 最后,将selectedItems数组绑定到需要提交的表单中,以实现多选的功能。 注意:如果需要限制已选项的个数,可以在selectedItems数组中添加一个限制条件。例如: ``` if (this.selectedItems.length === 3) { this.$message.warning('最多只能选择3个选项!'); return false; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值