V
V
Vitaly Pershin2019-01-18 17:41:55
Vue.js
Vitaly Pershin, 2019-01-18 17:41:55

How to display the text of the selected option?

I started to get acquainted with vuejs and I can’t figure out how to display the option text correctly, and not value.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-01-18
@Get-Web

data: () => ({
  options: [
    { value:  69, text:  'hello, world!!' },
    { value: 187, text:  'fuck the world' },
    { value: 666, text: 'fuck everything' },
  ],
  value: null,
}),
computed: {
  selectedText() {
    return (this.options.find(n => n.value === this.value) || {}).text || '';
  },
},

<select v-model="value">
  <option v-for="n in options" :value="n.value">{{ n.text }}</option>
</select>
<div>
  Текст выбранной опции: <span>{{ selectedText }}</span> 
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question