简体   繁体   中英

How to fix vmodel selected option vue js

So I have a web app where the user chooses from a list of options. Look at the following code:

 <div class="form-group"> <select v-model="étatArticle" class="form-control"> <option value="" disabled selected>Indique l'état de ton article</option> <option value="neuf">Neuf</option> <option value=tresBon>Trés bon état</option> <option value="bon">Bon état</option> </select> </div>

The problem is is that the v-model is causing the first selected option to disappear. to understand what I'm saying, look at the following images.

With v-model

Without v-model I get this enter image description here

can you please help me fix that? thank you.

In your component data, you need to define the default value of your v-model. You can see example in my codesandbox .

In your template:

<select v-model="selectedValue" class="form-control">
    <option value="" disabled selected>
      Disabled and selected by default
    </option>
    <option value="first">First</option>
    <option value="second">Second</option>
    <option value="third">Third</option>
  </select>

In your data:

  data() {
    return {
      // Define your value by default to show it
      selectedValue: "",
    };
  },

 new Vue({ template: '#main', data: { etatArticle: null } }).$mount('#app');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"></div> <template id="main"> <div class="form-group"> <select v-model="etatArticle" class="form-control"> <option:value="null" disabled selected>Indique l'état de ton article</option> <option value="neuf">Neuf</option> <option value=tresBon>Trés bon état</option> <option value="bon">Bon état</option> </select> </div> </template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM