Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

61
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda