简体   繁体   English

如何根据选中的复选框禁用复选框?

[英]How to disabled checkbox depending on which check box is selected?

Some fields need to be disabled depending on what is selected.根据所选内容,需要禁用某些字段。 If isTest and isFunctional are selected you should disable isTimed and reverse.如果选择了 isTest 和 isFunctional,您应该禁用 isTimed 和反向。 If isTimed is selected it should be disabled isTest and isFunctional.如果选择了 isTimed,则应禁用 isTest 和 isFunctional。 Check code:检查代码:

      <div class="checkbox">
        <label for="waiting"> waiting </label>
        <input class="input-control checkbox-box" [attr.disabled]="vajica" (change)="checkboxChanged($event)" formControlName="isTest" name="waiting" id="waiting"
          type="checkbox">
      </div>

      <div class="checkbox">
        <label for="functional"> functional </label>
        <input class="input-control checkbox-box" [attr.disabled]="vajica" (change)="checkboxChanged($event)" formControlName="isFunctional" name="functional" id="functional"
          type="checkbox">
      </div>

      <div class="checkbox">
        <label for="test"> test </label>
        <input class="input-control checkbox-box" [attr.disabled]="testica" (change)="checkboxChanged($event)" formControlName="isTimed" name="test" id="test" type="checkbox">
      </div>
  vajica: boolean = false;
  testica: boolean = false;




        checkboxChanged(e){

        console.log(e.target);
        console.log(this.exerciseAddForm); 
    
        if(this.exerciseAddForm.value.isTimed){
          alert('true')
          this.vajica = true;
        }
        if(this.exerciseAddForm.value.isTest ||this.exerciseAddForm.value.isFunctional) {
          this.testica = true;
        }
      }

First off I think it would be beneficial for you to look up how bind data to input fields in Angular. You shouldn't need to fire off a change event to capture and use data in Angular. In your case you would need to set those in your typescript file as data you can then bind to your input fields首先,我认为查看如何将数据绑定到 Angular 中的输入字段会对您有所帮助。您不需要触发更改事件来捕获和使用 Angular 中的数据。在您的情况下,您需要设置这些在您的 typescript 文件中作为数据,然后您可以绑定到您的输入字段

isTimed: boolean;
isTest: boolean;
isFunctional: boolean;

For your problem you'll need to add the disabled angular directive(? I forget what it's called) onto the input fields and attach the condition you're needing for it.对于您的问题,您需要将disabled的 angular 指令(?我忘了它叫什么)添加到input字段并附加您需要的条件。 For example, say you're trying to disable the isTimed field.例如,假设您正试图禁用isTimed字段。 Based on your rules can do something like the following.根据您的规则可以执行以下操作。 I have shortened the input field for readability.为了便于阅读,我缩短了input字段。

<input type="checkbox" [(ngModel)]="isTimed" [disabled]="isTest && isFunctional" />

This binds the isTimed variable as the model for your checkbox so when you click on the input checkbox isTimed will be set to true without the need for firing off any change events.isTimed变量绑定为checkbox的 model,因此当您单击input checkbox时, isTimed将设置为 true,而无需触发任何更改事件。 The [disabled] directive here will constantly monitor isTest and isFunctional and will disable the isTimed input the moment both are true.这里的[disabled]指令将持续监控isTestisFunctional ,并在两者都为真时禁用isTimed输入。

The syntax is different depending on the version of Angular you are using so I would look up how to "disable input fields with condition" for your version of Angular. Again look up how to bind data to input fields.语法因您使用的 Angular 版本而异,因此我会查找如何为您的 Angular 版本“使用条件禁用输入字段”。再次查找如何将数据绑定到输入字段。 It will make your job a lot easier它会让你的工作轻松很多

I shoud supose you're using Reactive Forms and that you has some like我应该假设你正在使用 Reactive Forms 并且你有一些像

myForm=new FormGroup({
  isTest:new FormControl(),
  isFunctional:new FormControl(),
  isTimed:new FormControl()
})

So, you has所以,你有

Updated we need take care with "disabled", some is disabled if we write [attr.disabled]="true" or if you write [attr.disabled]="false", so we need that if the condition is false the value must be null更新后我们需要注意“disabled”,如果我们写 [attr.disabled]="true" 或者如果你写 [attr.disabled]="false",一些被禁用,所以我们需要如果条件为 false 值必须是 null

[attr.disabled]="(condition) || null"

Just corrected刚刚更正

    <form [formGroup]="myForm">
          <div class="checkbox">
            <label for="waiting"> waiting </label>
            <input class="input-control checkbox-box" formControlName="isTest" 
              [attr.disabled]="myForm.get('isTimed').value|| null"
              name="waiting" id="waiting"
              type="checkbox">
          </div>
    
          <div class="checkbox">
            <label for="functional"> functional </label>
            <input class="input-control checkbox-box" formControlName="isFunctional" 
              [attr.disabled]="myForm.get('isTimed').value || null"
              name="functional" id="functional"
              type="checkbox">
          </div>
    
          <div class="checkbox">
            <label for="test"> test </label>
            <input class="input-control checkbox-box" formControlName="isTimed" name="test" 
             [attr.disabled]="(myForm.get('isTest').value && myForm.get('isFunctional').value) || null" 
             id="test" type="checkbox">
          </div>
    
    </form>

See that there's no (change) events or code in.ts.看到没有(更改)事件或代码 in.ts。 This make that if, eg you change hte values of the form by code, eg using pathValue这使得如果,例如,您通过代码更改表单的 hte 值,例如使用 pathValue

myForm.patchValue({isTest:true,isTimed:false})

The "checkboxs" are enabled acording the new values.根据新值启用“复选框”。

Updated 2 but this not change to true the value, so we need use "change".更新了 2但这并没有将值更改为 true,因此我们需要使用“更改”。 But in reactiveForms we can make some better than use change: subscribe to valueChanges of the control.但是在 reactiveForms 中我们可以做一些比使用 change 更好的事情:订阅控件的valueChanges As we don't want take tree subscriptions, we are going to use rxjs combineLastest (*) and write因为我们不想接受树订阅,所以我们将使用 rxjs combineLastest (*) 并编写

   combineLatest(
      this.myForm.get("isTest").valueChanges.pipe(startWith(this.myForm.value.isTest)),
      this.myForm.get("isFunctional").valueChanges.pipe(startWith(this.myForm.value.isFunctional)),
      this.myForm.get("isTimed").valueChanges.pipe(startWith(this.myForm.value.isTimed))
    )
      .subscribe(([isTest, isFunctional, isTimed]: [any, any, any]) => {
        const timedCtr = this.myForm.get("isTimed");
        const testCtr = this.myForm.get("isTest");
        const functionalCtr = this.myForm.get("isFunctional");
        if (isTimed) {
          testCtr.disable({ emitEvent: false });
          functionalCtr.disable({ emitEvent: false });
          testCtr.setValue(false, { emitEvent: false });
          functionalCtr.setValue(false, { emitEvent: false });
        } else {
          testCtr.enable({ emitEvent: false });
          functionalCtr.enable({ emitEvent: false });
        }
        if (isTest && isFunctional) {
          timedCtr.disable({ emitEvent: false });
           timedCtr.setValue(false, { emitEvent: false });
        } else timedCtr.enable({ emitEvent: false });
      });
  }

You can see in the stackblitz the two ways您可以在 stackblitz中看到两种方式

(*) combineLastest (observable1,observable2,...).subscribe([response1,response2,..]) make that we has in response1 the response of observable1, in response2 the response of observable2... but we need that all the observables has emitted some value. (*) combineLastest (observable1,observable2,...).subscribe([response1,response2,..])使我们在 response1 中有 observable1 的响应,在 response2 中有 observable2 的响应...但我们需要所有可观察量已经发出了一些价值。 this is the reason because we need use, startWith这是因为我们需要使用 startWith

Here is a simple solution.这是一个简单的解决方案。 By adding this listened for every checkbox will fix your problem:通过为每个复选框添加此监听将解决您的问题:

function checkClick(){
  
  let isFunctional = document.getElementById('functional')
  let isTest = document.getElementById('waiting')
  let isTimed = document.getElementById('test')

  if(isTest.checked && isFunctional.checked){
    isTimed.checked = false
    isTimed.disabled = true
  }else if(isTimed.checked){
    isFunctional.checked = false
    isFunctional.disabled = true
    isTest.checked = false
    isTest.disabled = true
  }else{
    isFunctional.disabled = false
    isTimed.disabled = false
    isTest.disabled = false
  }
  
}

You have to take in mind all the limit situations.你必须考虑所有的极限情况。 When unchecking you need to enable other checkboxes and so on.取消选中时,您需要启用其他复选框等。

Here is a Codeped link that you can play by your self with my solution.这是一个Codeped 链接,您可以使用我的解决方案自行播放。

Hope that helped:)希望有所帮助:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM