【问题标题】:v-for over array and set input [duplicate]v-for over 数组并设置输入
【发布时间】:2026-01-28 09:40:01
【问题描述】:

我正在循环我的模板中的一个 json,效果很好。

现在我有一个array,你可以在下面看到它,我想循环这个数组,并将第一个number输入到我的第一个b-form-inputv-model:value,第二个在第二个 b-form-input 等等。

所以我的 indexChild 等于应该输入的数字的位置。 (indexChild = 0 place of number in my array = 0 -> 输入:7011412)

我该如何解决?

<template>
  <div v-for="(item, indexChild) in json" :key="indexChild">
    <div class="mt-2">Input</div>
    <!-- v-for="item in array" :key="item" --> <!-- this doesn't work! -->
    <b-form-input v-model="???" :value="???" type="number"></b-form-input> 
  </div>
</template>

我的数组:

['7011412', '7012912', '7689012']

在控制台中看起来像这样:

【问题讨论】:

    标签: javascript vue.js vuejs2 v-for


    【解决方案1】:

    您可以将 v-model 和 value 设置为 json[indexChild]
    然后将您的 v-model 设置为给定索引处数组的值

    <div v-for="(item, indexChild) in json" :key="indexChild">
       <div class="mt-2">Input</div>
       <!-- v-for="item in array" :key="item" --> <!-- this doesn't work! -->
       <b-form-input v-model='json[indexChild]' :value="json[indexChild]" type="number"></b-form-input> 
    </div> 
    

    【讨论】: