简体   繁体   English

如何确定在Vue js中是否选中了复选框

[英]How to determine whether a checkbox is checked or not in Vue js

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');我只想确定在 Vue js 2 中是否选中了一个复选框。在 jquery 中,我们有像$('input[type=checkbox]').prop('checked');这样的函数 which will return true if checkbox is checked or not.如果复选框被选中或未选中,它将返回 true。 What is the equivalent function in Vue js. 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>

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) {
       alert('isSelected');
    }
}

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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM