简体   繁体   English

如何在选中复选框时将新值推送到数组中,并在使用 Vue.js 取消选中时删除?

[英]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.

相关问题 选中和取消选中时,将复选框值添加到数组中/从数组中删除复选框值(jQuery) - Add/remove checkbox values to/from array when checked and unchecked (jQuery) 选中复选框时将 object 推入数组,取消选中复选框时移除 object ReactJS - Push object in array when checkbox is checked and remove object when checkbox is unchecked in ReactJS 选中复选框时在阵列上推送数据,并在取消选中复选框时删除数据 - Push data on array when checkbox is checked and remove data when checkbox is unchecked 仅当检查材料UI复选框时,将值推入新数组 - push values in to new array only when material ui checkbox is checked 在 Vue 3 中选中和取消选中复选框时,如何执行两种不同的方法? - How to execute two different methods for when a checkbox is checked and unchecked in Vue 3? 取消选中复选框时,从收音机中删除“选中” - Remove “checked” from radio when a checkbox is unchecked 使用时,Vue.js输入(复选框收音机)不切换:与@input或@click一起检查 - Vue.js input (checkbox radio) does not toggle when using :checked with @input or @click together 选中/取消选中时填充/清除数组 - Populate/clear array when checkbox checked/unchecked 如何使用ajax或jquery选中和取消选中时设置复选框的值 - How to set value of checkbox when checked and unchecked using ajax or jquery Vue:选中/取消选中祖父母或父母时选中/取消选中复选框 - Vue: Check/uncheck checkbox when Grandparent or parent is checked/unchecked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM