目标:实现一个点选下拉菜单后,选择的菜单项显示在特定位置
原理:利用v-for 循环显示下拉菜单的内容,然后再利用v-model双向绑定数据
首先,做一个下拉菜单
<div id="app" >
<select >
<option > A</option>
<option > B/option>
<option > C</option>
</select>
</div>
利用V-for 循环下拉菜单选项
HTML:
<select >
<option v-for="option in list" v-bind:value="option.value">
{{ option.text }}
</option>
CSS:
new Vue({
el: '#app',
data: {
selected: 'A',
list: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}
})
用v-model 双向绑定下拉菜单
HTML:
<select v-model="selected">
<option v-for="option in list" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
<span>Selected: {{ selected }}</span>
完整代码
<div id="app" >
<select v-model="selected">
<option v-for="option in list" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
<span>Selected: {{ selected }}</span>
</div>
<script >
new Vue({
el: '#app',
data: {
selected: 'A',
list: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}
})
</script>