gpt4 book ai didi

javascript - Vue js判断复选框是否被选中

转载 作者:可可西里 更新时间:2023-11-01 01:58:37 25 4
gpt4 key购买 nike

我只想确定一个复选框是否在 Vue js 2 中被选中。在 jquery 中我们有像 $('input[type=checkbox]').prop('checked'); 如果复选框被选中,它将返回 true。 Vue js 中的等价函数是什么?

这是带有代码的场景。请注意,我使用的是 laravel 及其 Blade 模板。

@foreach ($roles as $role)
<input type="checkbox" v-on:click="samplefunction({{$role->id}})" v-model="rolesSelected" value="{{$role->id}}">
@endforeach

js部分是

<script>
var app = new Vue({
el: '#app1',
data: {
rolesSelected:"",
},
methods : {
samplefunction : function(value) {
// Here i want to determine whether this checkbox is checked or not
}
},
});

</script>

最佳答案

你可以这样做:

if(this.rolesSelected != "") {
alert('isSelected');
}

或者v-on:click="samplefunction({{$role->id}},$event)"

samplefunction : function(value,event) {
if (event.target.checked) {
alert('isSelected');
}
}

关于javascript - Vue js判断复选框是否被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com