0

I'm new with vue.js, and trying to find the best way to get a value from <option> of <select> element, and store it to data() property, so it can be passed to another component.

That <select> section looks like this:

<select ref="select">
  
<!-- key "item" == js object in array, fetched from API -->

  <option 
   v-for="(item, index) in arr"
  v-bind:key="index" 
  :value="item">{{item.val}}</option>
</select>

So, what's the best way to get item.val and put it into data() ?

Thanks in advance.

2 Answers 2

1
new Vue({
  template:'
  <select v-model="selectedOption" @change="handleChange">
          <option value="" disabled>--Select--</option>
          <option v-for="item in data" :value="item.val">{{item.text}}</option>
        </select>
',
data:{
   selectedOption:''
},
methods:{
handleChange:function(event){
  console.log(this.selectedOption); //you will find the value here
}
}
})
Sign up to request clarification or add additional context in comments.

Comments

0

The vue template:

<select v-model="selectVal" ref="select">
  
  <option 
   v-for="(item, index) in arr"
  v-bind:key="index" 
  :value="item">{{item.val}}</option>
</select>

The js code:

data: function(){
   return {
   
      selectVal: 0,

  };
},

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.