文章來源地址http://www.zghlxwxcb.cn/news/detail-714596.html
HTML:
<!--投票列表-->
<ul class="list f16">
<li v-for="(list,index) in listData" :key="index" >
<!--div @click="goAbout(list.orderid)">
img :src="list.thumb"
<span><i>編號:</i>{{list.orderid}}</span>
</div-->
<h6>姓名:{{list.tpname}}</h6>
<p>票數(shù):{{list.num}}</p>
<label>
<input type="checkbox" :value="list.orderid" v-model="checkGroup" @click="getCheck($event,list.orderid)">
<b>已選中</b>
</label>
</li>
</ul>
<!--投票按鈕-->
<div class="endButton" @click="checkVote">
<button>提交投票</button>
<div class="countNum" v-if="checkGroup.length > 0">請選擇<b>3</b>個,已選擇<b> {{checkGroup.length}} </b>個</div>
</div>
JS:
let checkGroup = ref([]) //選中數(shù)據(jù)
//選中 限制選中數(shù)量上限在選中時校驗
const getCheck =(event,orderid)=>{
//設(shè)置最多選擇三個
if(checkGroup.value.length < 3 && event.target.checked) {
checkGroup.value.push(orderid)
}else if(!event.target.checked){
checkGroup.value.splice(checkGroup.value.indexOf(orderid),1)
}else{
event.target.checked = false
//提示使用vantUi
proxy.$toast({
message: '已超出投票人數(shù)!',
icon: 'warning',
});
}
}
//提交投票 限制選中數(shù)量下限在提交時校驗
const checkVote=()=>{
//設(shè)置至少選擇三個
if(checkGroup.value.length <3){
proxy.$toast({
message: '至少選擇三個再投票',
icon: 'warning',
});
return
}
//其他代碼
if ("提交成功") {
//提示使用vantUi
proxy.$toast({
message: '投票成功!',
icon: 'smile',
});
//清除checkGroup選中數(shù)據(jù)
checkGroup.value = []
} else {
//提示使用vantUi
proxy.$toast({
message: response.data.info,
icon: 'warning',
});
//清除checkGroup選中數(shù)據(jù)
checkGroup.value = []
}
})
//其他代碼
}
CSS:
/*列表樣式*/
.list{display:flex;justify-content:space-between;flex-wrap:wrap;}
.list li{width:48%;background:#d71f18;border-radius:1vh;overflow:hidden;margin-top:2vh;color:#fff;text-align:center;padding-bottom:1vh;}
.list li div{position:relative;}
.list li span{display:block;position:absolute;left:1vh;bottom:1vh;background:rgba(0,0,0,0.6);padding:0 1.5vh 0 0;font-size:1.6vh;border-radius:1vh;overflow:hidden;color:#fff;}
.list li span i{display:inline-block;font-style:normal;background:#ac0006;padding:1vh 0 1vh 1vh;margin-right:1vh;}
.list li h6{padding:1.5vh 0 1vh 0;}
.list li p{padding:0 1vh;}
.list li label{display:block;background:#ffecd1;height:3.6vh;position:relative;border-radius:0.5vh;overflow:hidden;width:90%;margin:1.5vh auto 0 auto;}
.list li label:before{content:"\70b9\51fb\9009\4e2d";position:absolute;left:0;top:0;width:100%;text-align:center;color:#c0151b;line-height:3.8vh;}
.list li label input,.list li label input+b{display:none;}
.list li label input:checked+b{background:#ffcc00;display:block;position:absolute;left:0;top:0;width:100%;z-index:2;color:#c0151b;line-height:3.8vh;text-align:center;font-weight:normal;}
/*底部按鈕樣式*/
.endButton{position:fixed;left:0;bottom:0;width:100%;background:#ac0006;z-index:5;padding:1vh 0 1.5vh 0;color:#fff;text-align:center;}
.countNum{padding-top:1vh;font-size:1.4vh;}
.endButton button{border:none;background:none;font-size:2.4vh;font-weight:bold;}
文章來源:http://www.zghlxwxcb.cn/news/detail-714596.html
到了這里,關(guān)于Vue3:checkbox使用及限制選中數(shù)量的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!