【问题标题】:Send values into method in VueJS 2将值发送到 VueJS 2 中的方法
【发布时间】:2017-09-14 08:15:12
【问题描述】:

如何在下面的代码中的方法selectChange 中获取row.question_idoption.AnswerMasterID 的值?我正在使用 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>

我的方法如下:

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

我想获取option.AnswerMasterID 的值,它在内部循环中。

【问题讨论】:

  • 你到底是什么意思?由于您将 2 个参数传递给 selectChange 方法,因此您应该允许您的方法接受它们 selectChange(id, desc) { console.log(id, desc)} 并且它会记录触发方法时传递的数据。
  • 看起来不错。问题是什么?
  • 我想要 option.AnswerMasterID 的值
  • 你可以按照我解释的方式得到它们......

标签: vue.js vuejs2 v-for


【解决方案1】:

在 select 元素上使用 v-model 指令将数据属性绑定到它:

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

确保将selectedID 添加到数据属性中:

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

由于您已经将option.AnswerMasterID 绑定为每个选项的值,因此selectedID 将绑定到所选选项的AnswerMasterID

然后您可以在 selectChange 方法中像这样引用该值:

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

Here is Vue's documentation for select input binding.

【讨论】:

  • 这可行,但我对每个选择都有不同的值,并且每个选择的第一个选项值不同。因此,如果我写 selected : ' ' 和 v-model="selected" 那么我将获得空白作为所有选择元素的选定元素。如何解决?
  • @sagarmajumdar 这听起来像是一个不同的问题,我想你已经发布了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2018-09-21
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
相关资源
最近更新 更多