Home > front end >  How can I defined as Default checked in a checkbox with Vuejs
How can I defined as Default checked in a checkbox with Vuejs

Time:12-01

I have tried all that I have read in other questions and they did not answer me the problem.

I have a checkbox like this in a list

<input type="checkbox" v-model="selected" :value="post.rut">

I have the model defined like this:

data: function() {
        return {
            selected: []
        }
      }

The problem is that If I add checked as default.. it does not change at all I mean it keeps the checkbox not checked

  <input type="checkbox" v-model="selected" :value="post.rut" checked>

If I remove the v-model, it works BUT I can not send the value to the controller because I need the v-model to bind so I wonder how can I set as default checked in that checkbox input like that?

Thanks

CodePudding user response:

you have the input value as post.rut you can put it in selected array in data like :

data: function() {
    return {
        selected: [this.post.rut]
    }
  }

CodePudding user response:

If you already know which field you want to selected based on the post you can do like so:

data: function() {
        return {
            selected: [this.post.rut]
  • Related