在v-for中选择中选择框的第一个选项
Select first option of select box in in v-for
我想在下面的选择框中选择第一个选项。这两个我都试过了.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 () {
}
})
我该怎么做?我做错了什么?有更好的方法吗?
如果您的select绑定到您的v-model,您不需要选择任何特定的选项,Vue知道要将v-model中指定的keypath的值绑定到您在select中传递选项的value。
<option v-for="(timeIndex, time) in result.start_times" :value="timeIndex" >@{{ time.start_time }}-@{{ timeIndex }}</option>
http://vuejs.org/guide/forms.html#Select-选项
但是,您没有设置值。您正在绑定v-selected="timeIndex"。我以前从未见过v-selected,所以我在VueJS文档中查找它,它不会出现,所以除非它是自定义指令,否则它什么都不会做。
试试这个。可以将选定的属性绑定到表达式。如果索引为0,则为true,否则为false
<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" :selected="timeIndex === 0" >@{{ time.start_time }}-@{{ timeIndex }}</option>
</select>
</tr>
</table>
相关文章:
- 选择多个实例中的第一个
- 使用jquery选择元素附近的第一个h2
- jquery-select2显示默认选择的第一个值
- 使用jQuery从TR父级中选择第一个TD子级
- 文本框和选择框组合单个文本中的值(第一个tb值,然后是sb值)
- 如何在组合中选择第一个值
- 如何在数组的所有
中选择所有第一个 TD - 如何使用Jquery选择第一个td元素及其文本
- 不要使用jquery选择第一个li元素
- 如何选择第一个li
- 如何选择第一个td
- 数组总是选择第一个值,而不是特定的值
- 如何从循环中选择第一个
- jQuery上点击选择第一个类,而不是所有的类
- 始终选择第一个值Select vue.js
- 如何使用jQuery选择第一个父DIV
- 如何在元素的类定义列表中选择第一个类
- HTML中的TAB:默认选择第一个TAB
- 不能在json对象中选择第一个值
- 为什么它总是在AngularJS中选择第一个TD单元格