Javascript 如何确定在Vue js中是否选中了复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45559589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine whether a checkbox is checked or not in Vue js
提问by Geordy James
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.
我只想确定在 Vue js 2 中是否选中了一个复选框。在 jquery 中,我们有像$('input[type=checkbox]').prop('checked');这样的函数。如果复选框被选中或未选中,它将返回 true。Vue js 中的等价函数是什么。
Here is the scenario with code. Please note i am using laravel with its blade templates.
这是带有代码的场景。请注意,我使用 laravel 及其刀片模板。
@foreach ($roles as $role)
<input type="checkbox" v-on:click="samplefunction({{$role->id}})" v-model="rolesSelected" value="{{$role->id}}">
@endforeach
The js part is
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>
回答by madalinivascu
You can do something like:
您可以执行以下操作:
if(this.rolesSelected != "") {
alert('isSelected');
}
or
v-on:click="samplefunction({{$role->id}},$event)"
或者
v-on:click="samplefunction({{$role->id}},$event)"
samplefunction : function(value,event) {
if (event.target.checked)
}
回答by Raj
This worked for me.
这对我有用。
<input type="checkbox" :id="poid" class="unchecked" name="single_select" ref="rolesSelected">
function (){
if(this.$refs.rolesSelected.checked == false) {
//do something
}
}
function (){
if(this.$refs.rolesSelected.checked == true) {
//do something
}
}

