如何选择带有select的第一个项目(在vue.js中)?
<div class="ride-item-content">
<select v-model="ride.location_id">
<option v-for="location in locations" v-bind:value="location.id">
{{ location.from }} - {{ location.to }}
</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
JSON
{
"locations": {
"total": 2,
"per_page": 20,
"current_page": 1,
"last_page": 1,
"next_page_url": null,
"prev_page_url": null,
"from": 1,
"to": 2,
"data": [
{
"id": 1,
"user_id": 1,
"description": "sdg",
"from": "asdf",
"to": "asdf",
"kmz": "12",
"kmp": "13",
"time": 0,
"hour": 0,
"maps": "www.blablabla.nl",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"user_id": 1,
"description": "asdf",
"from": "asdf",
"to": "asdf",
"kmz": "3",
"kmp": "1",
"time": 1,
"hour": 0,
"maps": "www.test.nl",
"created_at": null,
"updated_at": null
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
- 编辑 -
<div class="ride-item-content">
<select v-model="ride.location_id">
<option v-for="location in locations" v-bind:selected="$index === 0 ? 'selected' : false">
{{ location.from }} - {{ location.to }}
</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
VueJS 1:
<option v-for="location in locations" v-bind:value="location.id" v-bind:selected="$index === 0 ? 'selected' : false">
{{ location.from }} - {{ location.to }}
</option>
Run Code Online (Sandbox Code Playgroud)
VueJS 2:
<option v-for="(location, index) in locations" v-bind:value="location.id" v-bind:selected="index === 0">
{{ location.from }} - {{ location.to }}
</option>
Run Code Online (Sandbox Code Playgroud)
在Vue 1中,循环$index内部自动可用v-for.
在Vue 2中,您需要为索引显式声明一个变量.
| 归档时间: |
|
| 查看次数: |
12210 次 |
| 最近记录: |