Setting a checkbox as checked with Vue.js

前端 未结 5 1970
清歌不尽
清歌不尽 2020-12-13 09:21

I have been googling and playing with every combination I know but I cannot get my checkboxes to be initialised as checked.

Example:

5条回答
  •  青春惊慌失措
    2020-12-13 10:11

    To set the value of the checkbox, you need to bind the v-model to a value. The checkbox will be checked if the value is truthy. In this case, you are iterating over modules and each module has a checked property.

    The following code will bind the checkbox with that property:

    
    

    Notice that I removed v-bind:value="module.id". You shouldn't use v-model and v-bind:value on the same element. From the vue docs:

    
    

    is just syntactic sugar for:

    
    

    So, by using v-model and v-bind:value, you actually end up having v-bind:value twice, which could lead to undefined behavior.

提交回复
热议问题