最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the list of all selected check boxes with Vue.js - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jan 28, 2022 at 21:11 Boussadjra Brahim 1 asked Sep 3, 2018 at 15:11 user1469734user1469734 80114 gold badges52 silver badges93 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 39

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:[]
   ....
   }
 }
发布评论

评论列表(0)

  1. 暂无评论