Javascript 对象中的vue.js循环选项值
我有这样的对象Javascript 对象中的vue.js循环选项值,javascript,vue.js,Javascript,Vue.js,我有这样的对象 fluit:[ {id:'001', name:"Apple"}, {id:'002', name:"Banana"}, {id:'003', name:"Mango"}, {id:'004', name:"orange"}, {id:'005', name:"papaya"}, ] 并在VueJ中编写如下代码 <select > <option v-for="(item, index) in fluit" :k
fluit:[
{id:'001', name:"Apple"},
{id:'002', name:"Banana"},
{id:'003', name:"Mango"},
{id:'004', name:"orange"},
{id:'005', name:"papaya"},
]
并在VueJ中编写如下代码
<select >
<option v-for="(item, index) in fluit" :key="index" :value="item.id" id="select"{{item.name</option>
</select>
对于您的解决方案,请使用此-
<template>
<select name="itemType" v-model="itemType" @change="onChange()" class="form-control">
<option value="001">Apple</option>
<option value="002">Banana</option>
</select>
</template>
<script>
export default {
data() {
return {
itemType: '',
}
},
methods: {
onChange() {
console.log('The new value is: ', this.itemType)
}
}
}
</script>
苹果
香蕉
导出默认值{
数据(){
返回{
项目类型:“”,
}
},
方法:{
onChange(){
console.log('新值为:',this.itemType)
}
}
}
我的建议是将vuetify(v-select)组件与vue一起使用,以获得更好的性能
<template>
<v-select
v-model="select"
:items="fluit"
item-text="id"
item-value="name"
label="Select"
@input="doSomething"
return-object/>
</template>
<script>
export default {
data () {
return {
select: { id:'005', name:"papaya" },
items: [
{id:'001', name:"Apple"},
{id:'002', name:"Banana"},
{id:'003', name:"Mango"},
{id:'004', name:"orange"},
{id:'005', name:"papaya"},
],
}
method:{
doSomething(){
console.log(this.select)
}
}
},
}
</script>
导出默认值{
数据(){
返回{
选择:{id:'005',名称:“木瓜”},
项目:[
{id:'001',名称:“苹果”},
{id:'002',名称:“Banana”},
{id:'003',名称:“Mango”},
{id:'004',名称:“橙色”},
{id:'005',名字:“木瓜”},
],
}
方法:{
doSomething(){
console.log(this.select)
}
}
},
}
我希望这会有帮助 对于您的解决方案,请使用此-
<template>
<select name="itemType" v-model="itemType" @change="onChange()" class="form-control">
<option value="001">Apple</option>
<option value="002">Banana</option>
</select>
</template>
<script>
export default {
data() {
return {
itemType: '',
}
},
methods: {
onChange() {
console.log('The new value is: ', this.itemType)
}
}
}
</script>
苹果
香蕉
导出默认值{
数据(){
返回{
项目类型:“”,
}
},
方法:{
onChange(){
console.log('新值为:',this.itemType)
}
}
}
我的建议是将vuetify(v-select)组件与vue一起使用,以获得更好的性能
<template>
<v-select
v-model="select"
:items="fluit"
item-text="id"
item-value="name"
label="Select"
@input="doSomething"
return-object/>
</template>
<script>
export default {
data () {
return {
select: { id:'005', name:"papaya" },
items: [
{id:'001', name:"Apple"},
{id:'002', name:"Banana"},
{id:'003', name:"Mango"},
{id:'004', name:"orange"},
{id:'005', name:"papaya"},
],
}
method:{
doSomething(){
console.log(this.select)
}
}
},
}
</script>
导出默认值{
数据(){
返回{
选择:{id:'005',名称:“木瓜”},
项目:[
{id:'001',名称:“苹果”},
{id:'002',名称:“Banana”},
{id:'003',名称:“Mango”},
{id:'004',名称:“橙色”},
{id:'005',名字:“木瓜”},
],
}
方法:{
doSomething(){
console.log(this.select)
}
}
},
}
我希望这会有帮助 在您的选择中添加一个v型,即:
<select v-model="selectedItem">
<option v-for="(item, index) in fluit" :key="index" :value="item.id" id="select">
{{item.name}}
</option>
</select>
{{item.name}
现在selectedItem将包含水果的值,例如:
在您的选择中添加一个v型,即:
<select v-model="selectedItem">
<option v-for="(item, index) in fluit" :key="index" :value="item.id" id="select">
{{item.name}}
</option>
</select>
{{item.name}
现在selectedItem将包含水果的值,例如:
这可能会对你有帮助:这可能会对你有帮助:嗨,亲爱的,谢谢你,我拿到了。你好,亲爱的,谢谢你,我知道了。你真是帮了大忙