检测复选框是否在 Angular.js ng-change 事件中被选中或取消选中 [英] Detect if checkbox is checked or unchecked in Angular.js ng-change event

查看:19
本文介绍了检测复选框是否在 Angular.js 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()" />

然后在控制器中我有:

$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.

推荐答案

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

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');
   }
}

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

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