在VUE(inertiajs) / tailwind中的复选框切换为“ true”始终提交。

发布于 2025-02-10 12:35:26 字数 1364 浏览 1 评论 0原文

我有一个内置在VUE(Ineriajs) /尾风中的切换复选框组件,每当我提交表单时,值始终是正确的。

这是组件的代码:

<template>
  <div class="form-check form-switch mt-5 form-inline first:mt-0">
    <label v-if="label" class="form-label sm:w-20">{{ label }} - this is {{ checked }}</label>
    <input type="checkbox" class="form-check-input mr-0 ml-3" :value="checked" :v-model="checked" :checked="checked" @change="onChange" :id="id"/>
  </div>
</template>

<script>
import { v4 as uuid } from 'uuid'
export default {
  props: {
    id: {
      default() {
        return `checkbox-${uuid()}`
      },
    },
    active: Boolean,
    error: String,
    label: String,
    checked: Boolean,
  },
  data() {
    return {
      checked: this.active
    }
  },
  methods: {
    onChange() {
      this.checked = !this.checked;
      this.$emit('input', this.checked);
    }
  },
}
</script>

并称为:


<Checkbox label="Active" :active="active" />

<script>
import Checkbox from '@/components/Checkbox/Checkbox'


export default {
  components: {
    Checkbox,
  },
  props: {
    user: Object,
    active: Boolean,
  },
  data() {
    return {
        active: this.user.active == 0 ? false : true,
    }
  },

</script>

切换时,正在更新检查值时,但是每当我提交表格时,提交的值始终是“ true”的,请任何人建议。

谢谢

I have a toggle checkbox component built in Vue (IneriaJS) / Tailwind, when ever I submit the form the value is always true.

Here is the code for the component:

<template>
  <div class="form-check form-switch mt-5 form-inline first:mt-0">
    <label v-if="label" class="form-label sm:w-20">{{ label }} - this is {{ checked }}</label>
    <input type="checkbox" class="form-check-input mr-0 ml-3" :value="checked" :v-model="checked" :checked="checked" @change="onChange" :id="id"/>
  </div>
</template>

<script>
import { v4 as uuid } from 'uuid'
export default {
  props: {
    id: {
      default() {
        return `checkbox-${uuid()}`
      },
    },
    active: Boolean,
    error: String,
    label: String,
    checked: Boolean,
  },
  data() {
    return {
      checked: this.active
    }
  },
  methods: {
    onChange() {
      this.checked = !this.checked;
      this.$emit('input', this.checked);
    }
  },
}
</script>

And is called:


<Checkbox label="Active" :active="active" />

<script>
import Checkbox from '@/components/Checkbox/Checkbox'


export default {
  components: {
    Checkbox,
  },
  props: {
    user: Object,
    active: Boolean,
  },
  data() {
    return {
        active: this.user.active == 0 ? false : true,
    }
  },

</script>

When toggling the checked value is being updated but whenever I submit the form the value submitted is always "true" please can anyone advise.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2025-02-17 12:35:26

您对发射什么都不做。将@input =“ yourcheckboxvariable = $ event”放在您的&lt;复选框.../&gt;中,并用变量做点事。 )

You do nothing with your emit. Put an @input="yourCheckboxVariable = $event" to your <Checkbox ... /> and do something with the variable. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文