action checkbox selected vue js
<template>
<div>
<input type="checkbox" v-model="isChecked" @change="handleCheckboxChange"> Check me
<p>Checkbox is checked: {{ isChecked }}</p>
</div>
</template>
<script>
export default {
data() {
return {
isChecked: false
};
},
methods: {
handleCheckboxChange() {
console.log('Checkbox changed');
}
}
};
</script>
Explanation:
- The
<input>
tag creates a checkbox. v-model="isChecked"
binds the checkbox state to theisChecked
data property. It ensures that the checkbox state reflects the value ofisChecked
and vice versa.@change="handleCheckboxChange"
listens for thechange
event on the checkbox and triggers thehandleCheckboxChange
method when the checkbox state changes.- Inside the
<script>
tag: - The
data()
function returns an object containing theisChecked
property initialized with a value offalse
. - The
handleCheckboxChange()
method logs a message to the console when the checkbox state changes. - The
{{ isChecked }}
expression in the paragraph tag displays the current value ofisChecked
, showing whether the checkbox is checked or not.