1

I am creating a list of check boxes via a result set. I can see the correct values but I am not able to set the value correctly

 <ul>
    <li v-for="role in roles">
        <input type="checkbox" :value="role.id"  v-model="form.roleIds" > {{role.name}}
    </li>
</ul>

When I click on one of the check boxes, I see all of them clicked.

enter image description here

That's what I see in console:

enter image description here

4
  • Whats form.roleIds? Commented Mar 17, 2020 at 11:35
  • form: { file: null, name: null, description: null, url: null, position: null, partnerId: null, roleIds: null, }, that's the next step, you can see the html generated, there is no value being set @Vucko Commented Mar 17, 2020 at 11:37
  • It's null so every chechkbox is binding to that null. Use an array reather. Look at the jsfiddle example how they bind the value to each object in the array. Commented Mar 17, 2020 at 11:40
  • @Vucko actually i wanted to show that form exists. I am not talking about server side now, I am getting the value as boolean instead of integer. Value is empty, you can see that. Commented Mar 17, 2020 at 11:42

2 Answers 2

1

I think your data property roleIds is not defined properly, it should be like this -

roleIds: []

Test.vue

<template>
  <ul>
    <li v-for="(role, i) in roles" :key="i">
      <label>
        <input type="checkbox" :value="role.id"  v-model="form.roleIds" >
        {{role.name}}
      </label>
    </li>
  </ul>
</template>

<script>
  export default {
    data: () => ({
      form: {
        roleIds: [] // **this is the catch**
      },
      roles: [{
        id: 1,
        name: 'Siddharth'
      },
      {
        id: 2,
        name: 'Arora'
      }]
    })
  }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Console the roles array and confirm there is value for id in every entries of roles. It seems like the value for id is null.

1 Comment

You are right, there was a jsonignore tag on the property which is why json couldn't get it

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.