0
<template> 
   <select class="form-control" id="sel2" v-model="genre_id">
              <option v-for="genre in genres" v-bind:key="genre.id">
                {{genre.name}}
              </option>
            </select
<template/>
<script>

    data() {
    return {
      genre_id: '',
}}
<script>

The code above gets all the genres and once one is picked it returns the genre.name. My question is, instead of returning the genre.name how can I return the genre.id even though I picked the name on my select form?

1 Answer 1

1

Add a value attribute:

<select class="form-control" id="sel2" v-model="genre_id">
  <option v-for="genre in genres" v-bind:key="genre.id" :value="genre.id">
    {{ genre.name }}
  </option>
</select>

The value attribute specifies the value to be sent to a server when a form is submitted.

The content between the opening and closing tags is what the browsers will display in a drop-down list. However, the value of the value attribute is what will be sent to the server when a form is submitted.

Note: If the value attribute is not specified, the content will be passed as a value instead.

Reference

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.