如何为v模型调用提供特定参数

时间:2018-03-28 17:51:13

标签: javascript vue.js vuejs2 v-model

如何在v-model中提供我的数组的索引?

<select v-model="dayList" id="dayList">
    <option v-for="(day, index) in days" :key="index">{{ selectDaysLabel(day) }}</option>
</select>

dayList(index) {
  console.log(index) // should give you either 0, 1, 2...
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以根据需要将<option>的值与特定数据类型绑定为v-bind:value="{ 'data': day, 'index':index }",或者只是v-bind:value="index"

new Vue({
    el: "#app",
    data: {
        selectedDay: '',
        days: ['A', 'B', 'C', 'D']
    },
    methods: {
      getSelect(){
        console.log(this.selectedDay ? this.selectedDay.index : 'No selection')
      }
    }
});
<script src="https://unpkg.com/vue@2.0.1/dist/vue.js"></script>
<div id="app">
<button v-on:click="getSelect()">Test</button>
<select v-model="selectedDay" >
    <option v-for="(day, index) in days" v-bind:value="{ 'data': day, 'index':index }">{{day}}</option>
</select>
</div>