0

How can I get the values of row.question_id and option.AnswerMasterID in the method selectChange in the below code? I am using Vue.js v2.

<div v-if="row.answer_input_type === 'Dropdown'">
  <template v-for="answer in answers">
    <template v-if="answer.AnswerTypeID === row.answer_type_id">
      <select class="dropdown_cl" v-bind:disabled="row.is_enabled == 1 ? false : true" @change="selectChange(row.question_id, answer.AnswerDescription)">
        <option v-for="option in answer.AnswerDescription" v-bind:value="option.AnswerMasterID"   >{{option.AnswerDescription}}</option>
      </select>
      <p v-for="option in answer.AnswerDescription">{{option.AnswerMasterID}}</p>
    </template>
  </template>
</div>

My method is as follows:

selectChange: function (question_id, ans_master_id) {
  alert(question_id + " " + ans_master_id)
},

I want to get the value of option.AnswerMasterID which is in the inner loop.

4
  • What you exactly mean ? Since you passed 2 parameters into selectChange method, you should allow your method to accept them selectChange(id, desc) { console.log(id, desc)} and It would log the data what's passed when method is fired. Commented Apr 18, 2017 at 13:10
  • Look ok. What is the question about? Commented Apr 18, 2017 at 13:10
  • I want the value of option.AnswerMasterID Commented Apr 18, 2017 at 13:16
  • and you can get them in way I explained... Commented Apr 18, 2017 at 13:20

1 Answer 1

1

Use a v-model directive on the select element to bind a data property to it:

<select class="dropdown_cl" v-model="selectedID" ...

Make sure you add selectedID to the data properties:

data() {
  return {
    selectedID: null,
  }
}

Since you are already binding option.AnswerMasterID as the value of each option, selectedID will be bound to the AnswerMasterID of the selected option.

You can then reference that value like this in your selectChange method:

selectChange: function (question_id) {
  alert(question_id + " " + this.selectedID);
},

Here is Vue's documentation for select input binding.

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

2 Comments

this works but I have different values for each select and the first option value for each is different. So if I write selected : ' ' and v-model="selected" then I am getting blank as the selected element for all the select elements. How to solve this?
@sagarmajumdar That sounds like a different question, which I think you've already posted?

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.