简体   繁体   English

Angular 7-如何禁用“保存”按钮,直到未填充“下拉列表中的值”和字段? 还是没有填写所有表格?

[英]Angular 7 - How to disable Save button until Value from Dropdown and field is not populated ? Or all form is not populated?

I already went through disable the button if the input is empty and How Do I disable the button if the input box is empty and enable when field is filled to perform my requirment, but I am using Picklist here. 如果输入为空我已经禁用了该按钮,如果输入框为空,我如何禁用该按钮如何在填充字段以执行我的要求时启用该按钮 ,但是我在这里使用的是Picklist。 There how can I make sure picklist and fields is not populated ? 我该如何确保未填充选择列表和字段? Until I really don't want to show add button. 直到我真的不想显示添加按钮。

<div class="zzz-input">
  <label class="zzz-inputbox-label" for="">
    Patient Role
  </label>
  <div class="input-container">
    <select id="" class="input-box" type="number" aria-required="true" [(ngModel)]="patient.roleTypeId"
      name="roleTypeId">
      <option *ngFor="let patientRole of patientRoles" value="{{patientRole.patientRoleId}}">{{patientRole.patientRoleName}}
      </option>
    </select>
  </div>
</div>

<div class="zzz-input">
  <label class="zzz-inputbox-label" for="">
    Patientnt Name
  </label>
  <div class="input-container">
    <input type="text" id="" class="input-box" aria-required="true" minlength="0" maxlength="100" autocomplete="off"
      width="0" min="" max="" step="" [(ngModel)]="patient.patientId" name="patientId">
  </div>
</div>
<div>
  <button class="zzz-btn zzz-btn-primary" title="Submit" aria-label="Save" (click)="addPatients()">
    Add
  </button>
</div>

This is why angular forms exist. 这就是为什么存在角形的原因。 I suggest to read the documentation on forms. 我建议阅读表格上的文档 I further suggest you dig into reactive forms, but here you are using ngModel, so let's make this form template driven. 我进一步建议您深入研究响应式表单,但是这里您使用的是ngModel,因此让我们将此表单模板驱动。 Just wrap your code inside form tags and make fields required. 只需将您的代码包装在表单标签中,然后将字段设为必填即可。 Here we name the form simply f . 在这里,我们将形式简单地命名为f Then disable the button with !f.valid or if you want to actually hide the button you can use *ngIf="f.valid" . 然后使用!f.valid禁用按钮,或者如果您想真正隐藏按钮,则可以使用*ngIf="f.valid" So here's a shortened modified code, where we disable the button if the form is not valid: 因此,这是一个简短的修改后的代码,如果表单无效,我们将在其中禁用按钮:

<form #f="ngForm" (ngSubmit)="onSubmit(f.value)">
  <select [(ngModel)]="patient.roleTypeId" name="roleTypeId" required>
    <! -- .... --->
  </select>
  <input [(ngModel)]="patient.patientId" name="patientId" required>
  <button [disabled]="!f.valid">
    Add
  </button>
</form>

Full code in STACKBLITZ STACKBLITZ中的完整代码

Use this - 用这个 -


  <button class="zzz-btn zzz-btn-primary" title="Submit" aria-label="Save" 
 [disabled]="patient.patientId && patient.roleTypeId" (click)="addPatients()">
    Add
  </button>

Hope it help! 希望对您有所帮助!

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

相关问题 Angular 2:在填充所有字段之前,表单有效返回 false - Angular 2: Form valid returns false until all fields are populated Angular 9 如果下拉菜单自动填充,则反应式表单会绕过表单验证 - Angular 9 reactive form bypasses form validation if dropdown automatically populated onclick 按钮弹出窗口打开。 弹出窗口包含输入字段。 保存弹出窗口时,应在多选下拉列表中填充值 - onclick of button popup opens. popup contains input field. On save of popup, the values should be populated in multiselect dropdown Angular 应用的下拉值未预先填充 - Angular app's dropdown value not pre-populated 订阅需要从从服务器获取的数组的第一项填充的表单字段数据 - Angular - Subscribe to form field data which needs to be populated from the first item of the array which is fetched from server - Angular 从JSON数据填充的下拉列表 - Dropdown populated from json data 表格未预填充 Angular 8 - Form not pre-populated Angular 8 如何在 Angular 6 HTML 中循环填充下拉列表? - How to make dropdown list populated in loop in Angular 6 HTML? 在angular 2表单中禁用默认的Dropdown值的提交按钮 - Disable the submit button for the default Dropdown value in an angular 2 Form Angular 禁用 Reactive Form 中的下拉菜单,直到设置另一个下拉菜单中的值 - Angular disable dropdown menu in Reactive Form until value in another dropdown menu has been set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM