检测angularjs ng-change事件中的复选框是否处于选中状态 [英] detect if checkbox is checked or unchecked in angularjs ng-change event

查看:325
本文介绍了检测angularjs ng-change事件中的复选框是否处于选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测在单击复选框时是否选中了该复选框.

I want to detect if a checkbox has been checked or unchecked when a click is happening on the checkbox.

这就是我拥有的:

<input type="checkbox" ng-model="answers[item.questID]" ng-change="stateChanged()" />

然后在控制器中,我有:

And then in the controller I have:

$scope.stateChanged = function () {
    alert('test');
}

我可以在选中/取消选中时触发警报,但是如何检测复选框的状态?我做了一些研究以找到类似的问题,但是我无法获得所需的东西.

I'm able to fire the alert when I do check/uncheck but how can I detect the state of the checkbox? I did research a bit to find a similar issue but I wasn't able to get what I need.

感谢Laziale

推荐答案

您可以只在ng-change方法中使用绑定的ng-model(answers[item.questID])值本身来检测是否已检查.

You could just use the bound ng-model (answers[item.questID]) value itself in your ng-change method to detect if it has been checked or not.

示例:-

<input type="checkbox" ng-model="answers[item.questID]" 
     ng-change="stateChanged(item.questID)" /> <!-- Pass the specific id -->

$scope.stateChanged = function (qId) {
   if($scope.answers[qId]){ //If it is checked
       alert('test');
   }
}

这篇关于检测angularjs ng-change事件中的复选框是否处于选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆