I just want to determine whether a checkbox is checked or not in Vue js 2. In jquery we have functions like $(‘input[type=checkbox]’).prop(‘checked’); which will return true if checkbox is checked or not. What is the equivalent function in Vue js.
Here is the scenario with code. Please note i am using laravel with its blade templates.
JavaScript
x
4
1
@foreach ($roles as $role)
2
<input type="checkbox" v-on:click="samplefunction({{$role->id}})" v-model="rolesSelected" value="{{$role->id}}">
3
@endforeach
4
The js part is
JavaScript
1
15
15
1
<script>
2
var app = new Vue({
3
el: '#app1',
4
data: {
5
rolesSelected:"",
6
},
7
methods : {
8
samplefunction : function(value) {
9
// Here i want to determine whether this checkbox is checked or not
10
}
11
},
12
});
13
14
</script>
15
Advertisement
Answer
You can do something like:
JavaScript
1
4
1
if(this.rolesSelected != "") {
2
alert('isSelected');
3
}
4
or
v-on:click="samplefunction({{$role->id}},$event)"
JavaScript
1
6
1
samplefunction : function(value,event) {
2
if (event.target.checked) {
3
alert('isSelected');
4
}
5
}
6