【发布时间】:2016-05-08 13:00:12
【问题描述】:
我想在下面的选择框中选择第一个选项。我已经尝试过 this.el.selected = true; 和
if (newValue == 0) return "selected";
但两者都不起作用。
<table class="table table-striped">
<tr v-for="result in results">
<select v-model='products[$index].startTime' class="form-control input">
<option v-for="(timeIndex, time) in result.start_times" v-selected="timeIndex" >@{{ time.start_time }}-@{{ timeIndex }}</option>
</select>
</tr>
</table>
Vue.directive('selected', {
bind: function () {
},
update: function (newValue, oldValue) {
if(newValue==0)
{
this.el.selected = true;
}
return "";
},
unbind: function () {
}
})
我该怎么做呢?我究竟做错了什么?有没有更好的办法?
【问题讨论】:
-
你可以在你的 v-for 循环中做 :selected="$index === 0" ,你不需要指令
标签: javascript vue.js