[Vuejs]-Vue.js radio button not checked by default

5👍

Don’t use :checked:

v-model will ignore the initial value, checked or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.

If v-model is the same as value it will return true for that checkbox. Your fixed fiddle:

new Vue({
  el: '#app',
  data: {
    photos: [{
        "id": "5bcebb6efeaea3147b7a22f0",
        "imgId": "12710.png",
        "visible": "all"
      },
      {
        "id": "5bcebbf0feaea3147b7a22f1",
        "imgId": "62818.png",
        "visible": "fav"
      },
      {
        "id": "5bcec010feaea3147b7a22f2",
        "imgId": "36740.png",
        "visible": "none"
      }
    ],
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">

  <ul v-for="(p, index) in photos">
    <li>

      <div>
        Visibility: {{p.visible}}
      </div>

      <strong>Visibility setting</strong><br>
      <input type="radio" v-model="photos[index].visible" :name=`visibility-${index}` value="all"> All <br>
      <input type="radio" v-model="photos[index].visible" :name=`visibility-${index}` value="fav"> My favorites <br>
      <input type="radio" v-model="photos[index].visible" :name=`visibility-${index}` value="none"> No one

    </li>
  </ul>



</div>

Now each radio group has it’s own name, with v-model targeting that group (note the index).

👤Vucko

Leave a comment