最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting initial value of a Vuetify checkbox - Stack Overflow

programmeradmin3浏览0评论

I have a Vue JS app that uses a Vuetify checkbox. I am unable to set the initial value to checked.

<v-checkbox
  v-else-if="input.type == 'checkbox'"
    false-value="0"
    true-value="1"
    v-model="input.val"
    :error-messages="form.errors[field]"
>

When input.val is equal to 1 i expect the checkbox to start off checked. However the checkbox will not start off checked.

I have also tried adding the props:

:value="input.val"
:input-value="input.val"

None of these affect the starting state of the checkbox though. If input.val starts off as 1, why does this box not start off checked?

<v-checkbox
  v-else-if="input.type == 'checkbox'"
  false-value="0"
  true-value="1"
  :value="input.val"
  :input-value="input.val"
  v-model="input.val"
  :error-messages="form.errors[field]"
>
  <template #label>@{{ input.hint }}</template>
</v-checkbox>

I have a Vue JS app that uses a Vuetify checkbox. I am unable to set the initial value to checked.

<v-checkbox
  v-else-if="input.type == 'checkbox'"
    false-value="0"
    true-value="1"
    v-model="input.val"
    :error-messages="form.errors[field]"
>

When input.val is equal to 1 i expect the checkbox to start off checked. However the checkbox will not start off checked.

I have also tried adding the props:

:value="input.val"
:input-value="input.val"

None of these affect the starting state of the checkbox though. If input.val starts off as 1, why does this box not start off checked?

<v-checkbox
  v-else-if="input.type == 'checkbox'"
  false-value="0"
  true-value="1"
  :value="input.val"
  :input-value="input.val"
  v-model="input.val"
  :error-messages="form.errors[field]"
>
  <template #label>@{{ input.hint }}</template>
</v-checkbox>
Share Improve this question edited Apr 28, 2023 at 16:39 Moritz Ringler 16.1k10 gold badges27 silver badges45 bronze badges asked Jul 7, 2019 at 0:50 VerySeriousSoftwareEndeavoursVerySeriousSoftwareEndeavours 1,7534 gold badges33 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You need to use : or v-bind for numbers, if you use v-bind:true-value it`s work fine. Example on codepen.

Example

<div id="app">
      <v-app id="inspire">
        <v-container fluid>
          <v-checkbox v-model="checkbox1"
                        v-bind:false-value="0"
                        v-bind:true-value="1"
                      :label="`Checkbox 1: ${checkbox1.toString()}`"></v-checkbox>
          <v-checkbox v-model="checkbox2" :label="`Checkbox 2: ${checkbox2.toString()}`"></v-checkbox>
        </v-container>
      </v-app>
    </div>

new Vue({
  el: '#app',
  data () {
    return {
      checkbox1: 1,
      checkbox2: 0
    }
  }
})

Try removing the false-value and true-value, and using false or true telling it if it should be checked in value.

发布评论

评论列表(0)

  1. 暂无评论