R
R
r_g_b_a2020-05-13 12:51:08
Vue.js
r_g_b_a, 2020-05-13 12:51:08

How to add a value attribute to only the first option tag?

Hello.
I'm trying to make a drop down list from the documentation. Following the recommendation, I want to add the disabled and value="" attributes to the first option . It turned out to make the item inactive, but how to display or hide attributes, in particular value , depending on the index ? My example
Once again the task in a nutshell: having an array

options: [
      "Learn JavaScript",
      "Learn Vue",
      "Play around in JSFiddle",
      "Build something awesome"
    ]

get exactly the same markup as in the documentation example.

UPD. I came up with such a working solution https://jsfiddle.net/24691wuc/ Is it possible to make it easier, or is this the best option?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-06-13
@r_g_b_a

<option
  v-for="(n, i) in options"
  v-text="n"
  v-bind="!i && { disabled: true, value: '' }"
></option>

I just can’t understand what is the point of disabling an option, the contents of which are quite meaningful? - if the user chose something else, and then decided to return to the first option, then this will not be possible. If you want to create something like a placeholder, then create a disabled option separately:
<option disabled value="">выбирай!</option>
<option v-for="n in options">{{ n }}</option>

D
drawnofmymind, 2020-05-13
@drawnofmymind

https://jsfiddle.net/2gxc8mz9/ so what?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question