[英]How to push new values into array when checkbox is checked and remove when unchecked using Vue.js?
I just started learning vue.js, I have such a problem, I have dynamic data which I render with 'v-for' loop, Also I have an empty array where I want to push checked checkbox data, and remove this data after uncheck, also I am trying to update new array if any of object data will change, for example, if I push first object into an empty array and then update the quantity value I don't want to push a new object But also want to update existing我刚开始学习 vue.js,我有这样的问题,我有动态数据,我用 'v-for' 循环渲染,还有一个空数组,我想推送选中的复选框数据,并在取消选中后删除这些数据,也如果 object 数据中的任何一个会发生变化,我正在尝试更新新数组,例如,如果我先将 object 推入一个空数组,然后更新数量值我不想推新的 object,但也想更新现有的
const app = new Vue({ el: '#app', data: () => ({ products:[ { itemId:'330012929', name:'testBook', cashBack:'3.00', price:12.99, quantity:1}, { itemId:'330012922', name:'testBook2', cashBack:'4.00', price:12.2, quantity:1}, ] }), methods:{ pushValue(arg){ console.log(arg) } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-for='(x,index) in products':key='index'> <div><span><input type='checkbox' @change='pushValue(x)'><span>item id: {{x.itemId}}</div> </div> </div>
<ul class="category__list">
<li v-for="(category, index) in categoriesData[categoryTitle]" :key="index">
<input type="checkbox" :value="category" v-model="selectedCategories">
{{category}}
</li>
</ul>
If you use v-model on checkbox input and declare it as array Vue will automaticly do the job for you.如果您在复选框输入上使用 v-model 并将其声明为数组,Vue 将自动为您完成这项工作。 SelectedCategories array will be checked boxes. SelectedCategories 数组将被选中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.