How can I get a list of all check boxes that I selected with Vue? This is my HTML which works and shows me a list of my products with a checkbox.
JavaScript
x
5
1
<li v-for="(product, index) in products">
2
<input :id="product.slug" :value="product.id" name="product" type="checkbox" />
3
<label :for="product.slug"><span></span></label>
4
</li>
5
What I want is that when I click on a button, it fetches all check boxes that I selected. And give me all the values.
But I can’t figure out how to do it, because it’ll break when I even try to add a v-model
to the checkbox.
Advertisement
Answer
Just bind every checkbox value
with a product and the v-model
to the array checkedProducts
JavaScript
1
14
14
1
<li v-for="(product, index) in products">
2
<input :id="product.slug" :value="product" name="product" type="checkbox" v-model="checkedProducts" />
3
<label :for="product.slug"><span></span></label>
4
</li>
5
6
7
data(){
8
return{
9
10
checkedProducts:[]
11
.
12
}
13
}
14