vue點(diǎn)擊標(biāo)簽切換選中及互相排斥操作
單身和已婚不能同時(shí)選中,不了解保險(xiǎn)和已了解保險(xiǎn)不能同時(shí)選中。
同時(shí)各個(gè)標(biāo)簽點(diǎn)擊可以取消選擇

//html
<li>
<span class="fill-title">與我相關(guān)</span>
<div>
<van-button
v-for="(item, index) in myself"
:key="index"
@click="checkButton('myself', item.id)"
:class="item.isFlag ? 'current' : ''"
>{{item.title}}</van-button>
</div>
</li>
<li>
<span class="fill-title">標(biāo)簽</span>
<div>
<van-button
v-for="item in biaoqian"
:key="item.id"
@click="checkButton('tag', item.id)"
:class="item.isFlag ? 'current' : ''"
>{{item.title}}</van-button>
</div>
</li>
數(shù)據(jù)
myself: [
{ id: 1,title: "親屬", isFlag: false },
{id: 2,title: "同鄉(xiāng)",isFlag: false},
{id: 3, title: "同學(xué)",isFlag: false },
{id: 4,title: "同事", isFlag: false},],
biaoqian: [
{id: 1, title: "已婚",type: 1,isFlag: false },
{id: 2,title: "單身",type: 1,isFlag: false },
{id: 3,title: "有娃",isFlag: false },
{id: 4,title: "有房", isFlag: false },
{id: 5,title: "有車",isFlag: false},
{id: 6,title: "不了解保險(xiǎn)",isFlag: false,type: 2},
{id: 7,title: "已了解保險(xiǎn)",isFlag: false,type: 2} ],
js
//標(biāo)簽只能選中一個(gè)
filterData(arr = [], index) {
let val = "";
arr.forEach(item => {
if (item.id == index) {
item.isFlag = !item.isFlag;
val = item.isFlag ? item.title : "";
} else {
item.isFlag = false;
}
});
return val;
},
checkButton(val, index) {
if (val === "tag") {
let data = [];
this.biaoqian.forEach(item => {
if (item.id == index) {
// a 記錄當(dāng)前標(biāo)簽狀態(tài)是否選中,為了取消標(biāo)簽狀態(tài)
let a = item.isFlag;
item.isFlag = !item.isFlag;
if (item.type) {
this.biaoqian.forEach(e => {
if ((e.type == 1 && index < 3) || (e.type == 2 && index > 5)) {
//先把同一個(gè)類型的標(biāo)簽都置為false
e.isFlag = false;
if (e.id == index) {
e.isFlag = a ? false : !e.isFlag;
}
}
});
}
}
});
let arr = this.biaoqian.filter(item => {
return item.isFlag;
});
arr.forEach(item => {
data.push(item.title);
});
this.personItem.labelList = data;
} else if (val === "sex") {
this.personItem.sex = this.filterData(this.sexArr, index);
} else {
this.personItem.relation = this.filterData(this.myself, index);
}
}
補(bǔ)充知識(shí):vue選中與取消簡(jiǎn)單實(shí)現(xiàn)
我就廢話不多說了,大家還是直接看代碼吧~
<li v-for="(item,index) in assign"
:key="index"
@click="selected(item)"
:class="{'active':item.isShow}">
selected(item) {
if (!item.isShow) {
item.isShow = true;
this.selectedList.push(item.id)
} else {
item.isShow = false;
let index = this.selectedList.indexOf(item.id);
if (index > -1) {
this.selectedList.splice(index, 1);
}
}
},
以上這篇vue點(diǎn)擊標(biāo)簽切換選中及互相排斥操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
17個(gè)vue常用的數(shù)組方法總結(jié)與實(shí)例演示
這篇文章主要介紹了vue中常用的數(shù)組方法,包括:VUE數(shù)組轉(zhuǎn)換字符串,VUE數(shù)組遍歷,VUE數(shù)組過濾,VUE數(shù)組查詢,VUE數(shù)組排序等功能,需要的朋友可以參考下2022-12-12
vue使用eventBus遇到數(shù)據(jù)不更新的問題及解決
這篇文章主要介紹了vue使用eventBus遇到數(shù)據(jù)不更新的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue實(shí)現(xiàn)tab導(dǎo)航欄并支持左右滑動(dòng)功能
本文給大家介紹利用Vue實(shí)現(xiàn)tab導(dǎo)航欄,并且通過flex布局實(shí)現(xiàn)左右滑動(dòng)效果,通過代碼給大家分享tab導(dǎo)航欄布局的實(shí)現(xiàn),本文給大家展示了完整代碼,需要的朋友參考下吧2021-06-06
解決v-for中使用v-if或者v-bind:class失效的問題
今天小編就為大家分享一篇解決v-for中使用v-if或者v-bind:class失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
關(guān)于Vue3中defineProps用法圖文詳解
在vue3中組件傳參有很多種方式,和v2大差不差,但是有的地方還是有很多的區(qū)別,下面這篇文章主要給大家介紹了關(guān)于Vue3中defineProps用法的相關(guān)資料,需要的朋友可以參考下2022-11-11

