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

196
Visualizações
validate if user select items or not in combobox in Vue js

I have combobox in Vue JS to select multiple items, I want to validate if user select any items or keep it empty, but the problem when I print the length and value of combox in console always gives length == 2 and values gives undefined in both cases if user select items or not

HTML

 <v-col cols="6">
        <v-combobox  class="xrfelements" :items="xrfElementsRatios" v-model="mainElementsRatios" :rules="notEmptyRule" label="Main Elements and Ratios" multiple required small-chips outlined dense></v-combobox>
  </v-col>

java script

validateForm: function (e) {
if (document.getElementsByClassName("xrfelements").length) {
console.log(document.getElementsByClassName("xrfelements".values
return true;}

 this.errors = [];
if (!document.getElementsByClassName("xrfelements").length){

       console.log(document.getElementsByClassName("xrfelements").index)
       console.log(document.getElementsByClassName("xrfelements".values)
        this.errors.push('there is no element selected.')
}
 

if (this.errors.length)
      

      {  
       this.$alert ("The following items should not be empty: " +this.errors.join(", "))

      }

    e.preventDefault(e);
      },
       
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's not how frameworks like vue work: You often times don't interact with the DOM directly. Instead, you use Vue intermediary. Please learn the basics of Vue.

new Vue({
    el: '#app',
    template: ` <v-col cols="6">
        <v-combobox  class="xrfelements" :items="xrfElementsRatios" v-model="mainElementsRatios" :rules="notEmptyRule" label="Main Elements and Ratios" multiple required small-chips outlined dense></v-combobox>
  </v-col>`,
    data() {
        return {
          notEmptyRule: [],
          mainElementsRatios: [],
          xrfElementsRatios: [],
        }
    },
    methods: {
        validateForm: function (e) {
            if (mainElementsRatios.length) {
                console.log(mainElementsRatios);
              return true;
            }

            this.errors = [];
            if (!mainElementsRatios.length) {

                console.log(document.getElementsByClassName("xrfelements").index)
                console.log(mainElementsRatios)
    this.errors.push('there is no element selected.')
}


            if (this.errors.length) {
                this.$alert("The following items should not be empty: " + this.errors.join(", "))

            }

            e.preventDefault(e);
        },
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></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