search

Home  >  Q&A  >  body text

Way to provide a unique ID for each input form generated by v-for in VueJS

<p>I'm trying to generate an input form for numbers (passed via props) and store the user input in the inputValues ​​array. My code is as follows: </p> <pre class="brush:php;toolbar:false;"><template> <div v-for="n in number" v-bind:key="n"> <input ref= "inputs" v-bind:id="'str' n" :v-model="inputValues[n]" /> </div> </template> export default defineComponent({ name: 'name', props: [ 'number', ], data() { return { inputValues: [] } } });</pre> <p>But nothing is stored in inputValues. What did i do wrong? Also, how do I give the input field a different id so that I can style it differently in CSS later? </p> <p>Edit: I managed to get it working! </p> <p> <pre class="snippet-code-html lang-html prettyprint-override"><code><div v-for="(n,i) in number" v-bind:key="n"> ; <input ref= "inputs" :id="'str' n" v-model="inputValues[i]" /> </div></code></pre> </p>
P粉245276769P粉245276769457 days ago544

reply all(1)I'll reply

  • P粉200138510

    P粉2001385102023-09-03 13:28:58

    I managed to make it work. v-bind: or ":" should not be used with v-model. I added the index because n starts at 1 instead of 0.

    <div v-for="(n,i) in number"  v-bind:key="n">
            <input ref= "inputs" :id="'str' + n" v-model="inputValues[i]" />
            
    </div>

    reply
    0
  • Cancelreply