Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

62
Vistas
Multiple checkboxes using v-for vue ts

I wanted to get the checkboxes values and store it in an array using v-model, the first instance I toggle a check it doesn't register but when I check a second one and hit submit the second value registers, another problem is that when I try to select multiple checkboxes it only logs the recently checked box after triggering the handleClick function. I've also tried console logging filter.filtProv and it only logs the recently checked.

//vue app
var filter = {
  filtProv: reactive([]) as String[]
}
var temp: any = []

let handleClick = () =>{
  temp.push(filter.filtProv)
  console.log(temp)
}


//template
<div id="checkboxes" v-for="provider in providerList" v-bind:key="provider" class="m-2">
   <input class="mx-1" type="checkbox" :value="provider" v-model="filter.filtProv"/>
   <label>{{provider}}</label>
</div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can achieve it by adding a checked parameter in your providerList objects.

For example :

{
  value: string,
  checked: boolean
}

And then at the end on submit you can filtered out the checked objects from the array.

Working Demo :

new Vue({
  el: '#app',
  data: {
    providerList: [{
        value: 'first checkbox',
      checked: false
    }, {
        value: 'Second checkbox',
      checked: false
    }, {
        value: 'Third checkbox',
      checked: false
    }, {
        value: 'Fourth checkbox',
      checked: false
    }]
  },
  methods: {
    submitForm() {
      const result = this.providerList.filter((obj) => obj.checked).map((obj) => obj.value);
      console.log(result);
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div id="checkboxes" v-for="(provider, index) in providerList" v-bind:key="index" class="m-2">
    <input class="mx-1" type="checkbox" :value="provider.value" v-model="provider.checked"/>
    <label>{{provider.value}}</label>
  </div><br>
  <button @click="submitForm()">Submit!</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda