Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the list of all selected check boxes with Vue.js

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.

<li v-for="(product, index) in products">
    <input :id="product.slug" :value="product.id" name="product" type="checkbox" />
    <label :for="product.slug"><span></span></label>
</li>

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.

like image 834
user1469734 Avatar asked Sep 03 '18 15:09

user1469734


People also ask

How do you check checkbox is checked or not in VUE JS?

prop('checked'); which will return true if checkbox is checked or not.

How do I uncheck all checkboxes in Vue?

updateCheckall – This method call when any checkbox checked state change. From here, change the state of check all checkbox. If all checkboxes are checked from the list then set checked to check all checkbox otherwise uncheck.


1 Answers

Just bind every checkbox value with a product and the v-model to the array checkedProducts

<li v-for="(product, index) in products">
    <input :id="product.slug" :value="product" name="product" type="checkbox" v-model="checkedProducts" />
    <label :for="product.slug"><span></span></label>
</li>

...
data(){
 return{
   ...
    checkedProducts:[]
   ....
   }
 }
like image 184
Boussadjra Brahim Avatar answered Sep 28 '22 06:09

Boussadjra Brahim