JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Setting up checkbox functionality in Vue

Author:JIYIK Last Updated:2025/03/01 Views:

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.


Using the v-model directive

The directive in Vue v-modelcan implement two-way data binding, thereby synchronizing the checkbox selection state with the data model. For example, we can define a checkbox component like this:

<template>
  <div>
    <label>
      <input type="checkbox" v-model="checked" />
      {{ label }}
    </label>
  </div>
</template>

<script>
export default {
  props: {
    label: String,
    value: Boolean,
  },
  data() {
    return {
      checked: this.value,
    };
  },
  watch: {
    checked(newVal) {
      this.$emit("input", newVal);
    },
  },
};
</script>

In this component, we use v-modelthe directive to bind the checked property and initialize the value of checked in data . When the user clicks the checkbox, the value of checked is automatically updated, triggering watchthe callback function in and passing the latest value $emitto the parent component through the method.

When using this checkbox component, we can write it like this:

<template>
  <div>
    <checkbox v-model="checked" label="选项1" />
    <checkbox v-model="checked" label="选项2" />
    <checkbox v-model="checked" label="选项3" />
  </div>
</template>

<script>
import Checkbox from "./Checkbox.vue";

export default {
  components: {
    Checkbox,
  },
  data() {
    return {
      checked: [false, false, false],
    };
  },
};
</script>

In this parent component, we use three checkbox components and v-modelbind them to the same array checked . Each element in this array corresponds to a checkbox. When the user selects a checkbox, the corresponding element in the checked array is automatically updated.


Use v-bind to bind the checked attribute

In addition to using v-modelthe directive, we can also use v-bindthe directive to bind the checked attribute to synchronize the checkbox's selection state with the data model. For example, we can define a checkbox component like this:

<template>
  <div>
    <label>
      <input type="checkbox" v-bind:checked="checked" v-on:change="handleChange" />
      {{ label }}
    </label>
  </div>
</template>

<script>
export default {
  props: {
    label: String,
    value: Boolean,
  },
  data() {
    return {
      checked: this.value,
    };
  },
  methods: {
    handleChange(event) {
      this.checked = event.target.checked;
      this.$emit("input", this.checked);
    },
  },
};
</script>

In this component, we use v-bindthe directive to bind the checked property and initialize the checked value in data . When the user clicks the checkbox, the change event is triggered, which calls the method and passes the latest value to the parent component through the method.handleChange$emit

When using this checkbox component, we can write it like this:

<template>
  <div>
    <checkbox v-bind:checked="checked[0]" v-on:input="checked[0] = $event.target.checked" label="选项1" />
    <checkbox v-bind:checked="checked[1]" v-on:input="checked[1] = $event.target.checked" label="选项2" />
    <checkbox v-bind:checked="checked[2]" v-on:input="checked[2] = $event.target.checked" label="选项3" />
  </div>
</template>

<script>
import Checkbox from "./Checkbox.vue";

export default {
  components: {
    Checkbox,
  },
  data() {
    return {
      checked: [false, false, false],
    };
  },
};
</script>

In this parent component, we use three checkbox components and bind their checked properties to the corresponding elements of the checked array. When the user selects a checkbox, the corresponding checked array element is automatically updated.


Summarize

This article introduces two methods of setting the checkbox function in Vuev-model : using the directive and using v-bindthe directive. Regardless of which method is used, it is necessary to synchronize the selection state of the checkbox with the data model to ensure the correctness of the data. In actual development, we can choose different methods according to specific needs and demonstrate them with actual examples.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

My understanding of webservice is this

Publish Date:2025/03/18 Views:150 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:62 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

How to redirect a website from HTTP to HTTPS

Publish Date:2025/03/16 Views:117 Category:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to avoid soft 404 in the website during SEO

Publish Date:2025/03/17 Views:136 Category:NETWORK

This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:179 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial