How to pass key as select option value in v-for when changed (VueJS)
P粉916553895
P粉916553895 2023-11-17 08:55:24
0
1
865

I'm using Vue 3 and Bootstrap 5.

I have a select with multiple options. Now I'm displaying my option.TEXT and when I change something, this value will also be passed into my method. But when I change something, I want to pass key (option.ID) .

But I also want my v-model to be my option.ID but show my option.TEXT.

How to achieve this without checking the method itself.

choose:

<select class="form-select" v-model="value" @change="get_key()">
    <option v-for="option in options" :key="option.ID">
        {{ option.TEXT }}
    </option>
</select>

Option array:

[
  {  "ID": 1,
     "TEXT": "One"
  },
  {  "ID": 2,
     "TEXT": "Two"
  },
  {  "ID": 3,
     "TEXT": "Three"
  },
]


P粉916553895
P粉916553895

reply all(1)
P粉848442185

option The property value should be bound to option.ID:

<select class="form-select" v-model="value" @change="get_key()">
    <option v-for="option in options" :key="option.ID" :value="option.ID">
        {{ option.TEXT }}
    </option>
</select>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template