【问题标题】:AngularJs: Unable to uncheck checkbox when uncheck another checkboxAngularJs:取消选中另一个复选框时无法取消选中复选框
【发布时间】:2017-04-14 06:36:31
【问题描述】:

有问题,这里的情况:
当我取消选中第二个复选框时,我想取消选中第一个复选框。

可以说,默认情况下,两个复选框都被选中。当我点击第二个复选框时,我希望第一个复选框也取消选中。

HTML:

<ion-checkbox ng-model="firstCheckbox">First Checkbox</ion-checkbox>
<ion-checkbox ng-model="secondCheckbox" ng-change="uncheckFirstCheckbox(secondCheckbox)">Second Checkbox</ion-checkbox>


JS:

    $scope.uncheckFirstCheckbox = function(secondCheckbox) {
        if(secondCheckbox.checked === false) {
            $scope.firstCheckbox = false;
        };
    };


$scope.firstCheckbox 变为 false,但在 HTML 中仍未选中。
我可以知道问题出在哪里吗?

【问题讨论】:

  • 试试if(!$scope.secondCheckbox) { $scope.firstCheckbox = false; };,不要将函数调用中$scope上的值作为一个好的做法来传递
  • 感谢您的提示 :) @entre

标签: angularjs checkbox ionic-framework


【解决方案1】:

您正在尝试访问 secondCheckbox.checked 属性,但 secondCheckbox 已经是 false。您应该只检查 secondCheckboxfalse 值。使用if(!secondCheckbox) 代替条件。

Here 是您的代码的工作小提琴。

HTML

<ion-checkbox ng-model="firstCheckbox">First Checkbox</ion-checkbox>
<ion-checkbox ng-model="secondCheckbox" ng-change="uncheckFirstCheckbox(secondCheckbox)">Second Checkbox</ion-checkbox>

JS

$scope.uncheckFirstCheckbox = function(secondCheckbox) {
    if (!secondCheckbox) {
        $scope.firstCheckbox = false;
    };
};

【讨论】:

  • 感谢您的回答! @Divyanshu
猜你喜欢
  • 1970-01-01
  • 2018-04-15
  • 2021-12-23
  • 2020-03-16
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多