简体   繁体   中英

How to set the option selected in a select field with vue?

<span class="input-group-btn" style="min-width: 100px">
            <select class="form-control" v-model="field.related_field">
                <option value="null"></option>
                <option v-for="f in fields" v-bind:value="f.id" v-if="f.id != ''" v-bind:selected="f.id == field.related_field ? true : false">{{ f.label }}</option>
            </select>
        </span>

This is my code in vue js. I am trying to set the option selected if the user submitted anything before when I am fetching from database. I tried to bind with the selected attribute. Can anyone help with this, please?

v-model takes care of this for you. Just set the expression you are binding with v-model to the value you want.

this.field.related_field = <value you want to default to>

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