search

Home  >  Q&A  >  body text

javascript - After the value of the checkbox is bound to the instance on Vue, it cannot be selected by default (checked is invalid)

<p id="form-1">
    <input type="checkbox" id="checkbox" v-model="toggle" :true-value='a' :false-value='b'>
    <label for="checkbox">{{ toggle }}</label>
</p>
    

JS

new Vue({
            el:'#form-1',
            data:{
                toggle:true,
                a:'选了',
                b:'没选'
            }
        })

Now I want this check box to be selected by default and the value is 'selected'. How should I write it?

滿天的星座滿天的星座2776 days ago654

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:33:41

    <input type="checkbox" id="checkbox" v-model="toggle">
    <label for="checkbox">{{ toggle ? '选中' : '未选中' }}</label>
    new Vue({
      el: '#form-1',
      data: {
        toggle: true
      }
    })

    reply
    0
  • Cancelreply