Way to provide a unique ID for each input form generated by v-for in VueJS
P粉245276769
P粉245276769 2023-09-02 14:58:19
0
1
537
<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粉245276769
P粉245276769

reply all(1)
P粉200138510

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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template