var app = new Vue({
el: '#app',
data: {
isActive : false,
chkGenres : [],
genres : [
{ "id" : "1", "name" : "Apple" },
{ "id" : "2", "name" : "Banana" },
{ "id" : "3", "name" : "Peach" }
]
},
methods: {
isChecked(){
this.isActive = ! this.isActive;
}
}
});
Please check my demo
https://jsfiddle.net/byxda8eq/9/
I want to toggle class "active" to li tag when specified checkbox is checked or not.
I can not find a way to do that cuz my brain does not working everyday.
How to?
The issue here is that you are using a single isActive
data option and binding it to all the checkboxes. So, when if any one of them is checked, it toggles the isActive
value and is it bound to all of them thus all of the li class are changed.
A simple way to resolve this by adding a new property isActive
to all the objects inside the genres
array like:
genres : [
{ "id" : "1", "name" : "Apple", isActive: false },
{ "id" : "2", "name" : "Banana", isActive: false },
{ "id" : "3", "name" : "Peach", isActive: false }
]
and then update the template like:
<li v-for="genre in genres" :class="{ active : genre.isActive }">
and then update the click method like:
@click="isChecked(genre)"
and the main method like:
isChecked(genre){
genre.isActive = !genre.isActive;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.