Vue.js 设置值时如何更改Vue中的输入值,并在它是数组的对象时更新它?
我在文章循环数组中生成了很多输入框。 文章是对象数组。输入框的值在每个对象中用“值”键设置Vue.js 设置值时如何更改Vue中的输入值,并在它是数组的对象时更新它?,vue.js,Vue.js,我在文章循环数组中生成了很多输入框。 文章是对象数组。输入框的值在每个对象中用“值”键设置 <div v-for="(option, i) in this.article" :key="i" class="form-row" > <div class="col"> <input v-model="option.va
<div
v-for="(option, i) in this.article"
:key="i"
class="form-row"
>
<div class="col">
<input
v-model="option.value"
:label="'label '+i"
:name="'inputValue'+i"
type="text"
required
/>
</div>
</div>
<button @click="submit"></button>
<script>
export default {
name: 'Article',
data(){
return(
article:[
{id: 'art1', value: 'artValue1'},
{id: 'art2', value: 'artValue2'},
{id: 'art3', value: 'artValue3'}
// There are about 50 objects
]
)
},
methods:{
submit(){
// How get values of inputs?
}
}
}
</script>
导出默认值{
名称:'文章',
数据(){
返回(
第条:[
{id:'art1',value:'artValue1'},
{id:'art2',value:'artValue2'},
{id:'art3',value:'artValue3'}
//大约有50个物体
]
)
},
方法:{
提交(){
//如何获取输入值?
}
}
}
如何在vue中更改输入值并更新对象?您忘记了从
数据函数返回对象,您可以将选项传递到提交处理程序并打印输入值
<template>
<div class="input-bound-object-array">
<div class="row">
<div class="col-md-6">
<form @submit.prevent="submitForm">
<div class="form-group" v-for="(option, i) in article" :key="i">
<label>{{ option.id }}</label>
<input type="text" class="form-control" v-model="option.value"
required />
</div>
<button type="submit" class="btn btn-secondary">Submit</button>
</form>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
article: [
{ id: 'art1', value: 'artValue1' },
{ id: 'art2', value: 'artValue2' },
{ id: 'art3', value: 'artValue3' }
// There are about 50 objects
]
}
},
methods: {
submitForm() {
// 'this.article' is updated as the input values are changed, so at this point
// you have the data changes, so you can process it as needed, i.e. POST to REST endpoint
console.log(this.article);
}
}
}
</script>
//如何获取输入值?->你是指所有的输入,或者按钮点击的输入,或者其他任何东西。下面的代码用于处理单个输入和提交处理程序
导出默认值{
名称:'文章',
数据(){
return{//您错过了这个
第条:[
{id:'art1',value:'artValue1'},
{id:'art2',value:'artValue2'},
{id:'art3',value:'artValue3'}
]
}
},
方法:{
提交(选项){//当前选项
console.log(option.id,option.value)
},
亚高(){
console.log(this.article)//将更新输入值作为
您正在使用'v-model',请检查'value'`
}
}
}
演示链接我根据您的代码创建了一个示例单文件组件。看看我的修改。Vue反应性的“魔力”在于,当您更改输入值时,“文章”数据会实时更新
<template>
<div class="input-bound-object-array">
<div class="row">
<div class="col-md-6">
<form @submit.prevent="submitForm">
<div class="form-group" v-for="(option, i) in article" :key="i">
<label>{{ option.id }}</label>
<input type="text" class="form-control" v-model="option.value"
required />
</div>
<button type="submit" class="btn btn-secondary">Submit</button>
</form>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
article: [
{ id: 'art1', value: 'artValue1' },
{ id: 'art2', value: 'artValue2' },
{ id: 'art3', value: 'artValue3' }
// There are about 50 objects
]
}
},
methods: {
submitForm() {
// 'this.article' is updated as the input values are changed, so at this point
// you have the data changes, so you can process it as needed, i.e. POST to REST endpoint
console.log(this.article);
}
}
}
</script>
{{option.id}
提交
导出默认值{
数据(){
返回{
第条:[
{id:'art1',value:'artValue1'},
{id:'art2',value:'artValue2'},
{id:'art3',value:'artValue3'}
//大约有50个物体
]
}
},
方法:{
submitForm(){
//“this.article”会随着输入值的更改而更新,因此此时
//您有数据更改,因此可以根据需要对其进行处理,即POST到REST端点
console.log(本文);
}
}
}
您不需要将“this”与“article”一起使用。在文章中使用v-for=“(选项,i)”
。另外,我很确定
标记没有“label”属性。@Tim label不是问题所在。请忽略它。我需要一种类似“序列化”的方式,但不知道如何在Vue中实现。我看到您今天发布了一个新的Vue问题。你看过我对这个问题的回答了吗?我认为它解决了这个问题。是的,我在这里投票给你。但在那个问题上,当我使用计算机时,它起了作用。一开始我没用过,我很感激你的支持。我想我的意思是,如果我的答案回答了这个问题,那么请接受答案。但如果没有,那没问题。谢谢你的评论,我错过了回复。我编辑过,但我想你没有理解我真正的问题。我需要得到50个输入的值。如何获取它?您可以使用this.article
、console.log(this.article)//当您使用v-model