1

我正在遍历一组键值以创建一个复选框列表,每个复选框都有一个同级禁用输入。选中每个复选框后,同级输入文本字段将启用并且是必需的。在此视图中,有一个“上一个”和“下一个”按钮,如果用户选择了一个复选框,然后没有在其所需的同级输入中输入任何内容,则应该禁用“下一个”按钮。我几乎可以正常工作,但是一旦用户选中该框,“下一步”按钮就应该被禁用,因为这意味着他们没有在所需的文本输入中输入任何内容。现在,“下一步”按钮只有在用户选中复选框时才会被禁用,专注于兄弟输入,然后不输入就离开。

我的 HTML...

<div *ngFor="let promotion of promotionOptions; let i = index">
    <div class="col-md-6 input-container radio-label">
        <mat-checkbox [checked]="!promotion.key" (change)="promotion.key = !promotion.key">
            {{ promotion.name }}
        </mat-checkbox>
    </div>
    <mat-input-container>
        <input matInput [disabled]="promotion.key" placeholder="Cost" name="promotionCost{{i}}" #promotionCost="ngModel" [ngModel]="" (keyup)="promotionCostInput($event.target.value)"
            [required]="!promotion.key" type="number">
        <div *ngIf="promotionCost.errors && (promotionCost.dirty || promotionCost.touched)" class="alert alert-danger cost-alert">
            <div [hidden]="!promotionCost.errors.required">Please enter the checked promotion's cost</div>
        </div>
    </mat-input-container>
</div>

<div class="clearfix"></div>

<div class="button-container">
    <button class="main-btn dark icon-left" (click)="updateStep(1)"><i class="fa fa-angle-left"></i>Previous</button>
    <button class="main-btn icon-right" (click)="updateStep(3)" [disabled]="!promotionCostValid">Next<i class="fa fa-angle-right"></i></button>
</div>

以及我在 .ts 文件中使用的禁用“下一步”按钮的方法:

promotionCostInput(value) {
    if (!value) {
      this.promotionCostValid = false;
    } else {
      this.promotionCostValid = true;
    }
  }

当用户选中复选框时,如何验证同级输入?

4

1 回答 1

1

您的问题是按钮的状态仅在您的任何输入触发事件next时才会更新。keyup此外,它仅使用一个输入的值进行更新,但根据您所说的,您要检查您的每个输入是否都已ngFor填充。

我建议您将输入值存储在模型中,并在输入更改或复选框被选中时检查促销成本是否对所有促销有效。

<div *ngFor="let promotion of promotionOptions; let i = index">
  <div class="col-md-6 input-container radio-label">
    <mat-checkbox [checked]="!promotion.key" (change)="promotion.key = !promotion.key; checkPromotionCost();">
      {{ promotion.name }}
    </mat-checkbox>
  </div>
  <mat-input-container>
    <input
      matInput
      [disabled]="promotion.key"
      placeholder="Cost"
      name="promotionCost{{i}}"
      (keyup)="promotion.cost = $event.target.value; checkPromotionCost();"
      [required]="!promotion.key" type="number"
    >
    <div *ngIf="promotionCost.errors && (promotionCost.dirty || promotionCost.touched)" class="alert alert-danger cost-alert">
      <div [hidden]="!promotionCost.errors.required">
        Please enter the checked promotion's cost</div>
      </div>
  </mat-input-container>
</div>

<div class="clearfix"></div>

<div class="button-container">
  <button class="main-btn dark icon-left" (click)="updateStep(1)"><i class="fa fa-angle-left"></i>Previous</button>
  <button class="main-btn icon-right" (click)="updateStep(3)" [disabled]="!promotionCostValid">Next<i class="fa fa-angle-right"></i></button>
</div>

在控制器中:

checkPromotionCost() {
  this.promotionCostValid = true;
  this.promotionOptions.forEach(promotion => {
    if (promotion.key && promotion.cost === '') {
      this.promotionCostValid = false;
    }
  });
}
于 2017-11-14T16:05:50.273 回答