首页 > 解决方案 > 如何使用角度6根据循环中的特定值禁用复选框?

问题描述

如果值 = 2,我想禁用复选框。这是我迄今为止尝试过的。

discheckcondition = [1,2,3];

for (let x = 0; x < this.discheckcondition.length; x++) {
    // console.log(this.discheckcondition[x]);
    if (this.discheckcondition[x] = '2') {
        console.log(this.discheckcondition[x]);
        this.disablecheckfornext = '1';
    };
}
<ng-container *ngFor="let value of values;">
 <label>
  <input id="checkbox_group1" type="checkbox" pattern="[0-9]{10}" [disabled]=" disablecheckfornext == '1'"  value="value.id" (change)="onCheckboxChange($event)"/>
 </label>
</ng-container>

有人可以帮助我吗?这里的问题是所有文本框都被禁用了。

标签: javascriptangularangular6

解决方案


如果 Checkbox 在 ngFor 循环中,此代码将起作用

discheckcondition = [1,2,3];

for (let x = 0; x < this.discheckcondition.length; x++) {   
        this.disablecheckfornext[x] = false;
    if (this.discheckcondition[x] = '2') {       
        this.disablecheckfornext[x] = true;
    };
}
<input id="checkbox_group1" type="checkbox" pattern="[0-9]{10}" [disabled]=" disablecheckfornext[#i]"  value="" (change)="onCheckboxChange($event)"/>


推荐阅读