1

I want to generate radio buttons in vue.js by v-for, but when they are generated if i selected one radio the rest radios is selected too, so i want user to be able to choose only one radio.'

<template>
 <div> 
  <div v-for="item in items">
      <input type="radio" id="l1"  value="item.text" v-model="checked">
      <label id="l1" for="item.text">{{item.text}}</label>
  </div>
 </div> 
</template>
1
  • 1
    Add a colon : before for to tell Vue to interpret its contents as a js expression instead of as a string: <input type="radio" :for="item.text">. Do the same for :value="item.text". It's a shorthand for v-bind:for and v-bind:value. Commented Dec 22, 2020 at 10:00

1 Answer 1

2

You should bind value.

As you didn't bind value, radio button values are all same as the fixed value - item.text, not the dynamic one ${item.text} which is the text value of item object.


for attribute of label tag is the id of the form element to be bound.

  <div v-for="item in items">
      <input type="radio" :id="item.text" :value="item.text" v-model="checked"> // `:value` is shorthand for `v-bind:value`
      <label :for="item.text">{{item.text}}</label>
  </div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.