简体   繁体   English

使用angular8取消选中复选框时如何保持禁用输入

[英]How to keep input disabled when the checkbox is unchecked using angular8

i have used Reactive forms in Angular8 for form binding.我在 Angular8 中使用了 Reactive forms 进行表单绑定。 Here, initially the form is disabled, and when i click on edit button, the form is enabled, but here, the input should be enabled only if the checkbox is checked, or it should be disabled.在这里,最初表单被禁用,当我单击编辑按钮时,表单被启用,但是在这里,只有在选中复选框时才应该启用输入,或者应该禁用它。

Ts: TS:

 edit() {
    this.form.enable()
  }
  restrict() {
      this.form = this.FB.group({
      array: this.FB.array(
        this.values.map(x => this.FB.group({
          boolValue: this.FB.control(x.boolValue),
          datetime: [
          { value:x.datetime, disabled: !x.boolValue }]
        }))
      )
    });    
     this.form.valueChanges.subscribe(data => {
          this.formEdit = true;
          console.log('agentSettingsInfoForm', data,this.formEdit)
        })
     this.w9InfoDetails.valueChanges.subscribe(data => {
        this.formEdit = true;
          console.log('agentSettingsInfoFormdate', data,this.formEdit)
     }) 
  }

DEMO 演示

Tweaked your code in stackblitz example here are the change you need to make in order to achieve what you expect.在 stackblitz 示例中调整了您的代码,这里是您需要进行的更改才能实现您的期望。

Wrap your whole form inside <fieldset> and set it disabled by default.将整个表单包裹在<fieldset>中,并默认将其设置为禁用。

<fieldset [disabled]="disabled">
 <form [formGroup]= "form" (submit)="onSubmit()">
  ......

In component assign default value to disabled在组件中将默认值分配给disabled

disabled = true; // set default disabled true
form: FormGroup;    
selected: string[] = [];
......

Remove form disable from ngOnInit从 ngOnInit 移除表单禁用

 ngOnInit() {
  this.restrict();
  // this.form.disable() <== remove it
 }   

And finally in edit method set disabled false最后在编辑方法中设置禁用假

  edit() {
   this.disabled = false;
   // this.form.enable() <== remove this
  }

Here is the stackblitz DEMO I updated your code这是我更新了您的代码的 stackblitz DEMO

Hope this solve your issue.希望这能解决您的问题。

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

相关问题 如何使用 Angular8 检测输入的变化 - How to detect change in input using Angular8 Angular8 表单输入字段被禁用 - Angular8 form input fields disabled 选中和取消选中复选框上的启用和禁用文本输入 - enabled and disabled text input on checkbox checked and unchecked 如何使用angular8中的反应形式选择和取消选择复选框 - how to select and deselect checkbox using reactive forms in angular8 如果其他控件没有值,如何使复选框保持禁用状态,如果控件在 angular8 中有值则启用 - How to make checkbox remain disabled if other control doesnt have value and enable if control has value in angular8 当在角度中选中其他复选框时如何取消选中复选框 - how to make checkbox unchecked when other checkbox is checked in angular 如何使用angular8限制输入字段中的保留字符 - How to restrict reserved characters in input field using angular8 当使用jQuery取消选中子复选框时,如何取消选中父复选框? - How to unselect the parent checkbox when their child checkbox is unchecked using jQuery? 在 Angular 中选中/取消选中复选框时如何调用不同的方法? - How to call different methods when checkbox is checked/unchecked in Angular? 当字段为空时如何使用 angular8 禁用按钮 - How to disable the button when fields are empty using angular8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM