简体   繁体   English

提交后如何禁用按钮

[英]How disable button after submit

I develop a form to add users, the form works, but the problem is when click on the button submit two successive times, the method add two users, I want the button submit (add) disable after click directly ( I work with angular 5) 我开发了一个添加用户的表单,该表单有效,但是问题是当连续两次单击按钮提交时,该方法添加了两个用户,我希望直接单击后禁用按钮提交(添加)(我使用角度5 )

HTML Code : HTML代码:

 <form [formGroup]="addfrm"> ... ... <div style="text-align:center"> <button class="btn btn-primary" (click)="processAdd()" [disabled]="addfrm.invalid">add</button> <button data-dismiss="modal" class="btn btn-default">cancel</button> </div> </form> 

You can try with ngForm directive : 您可以尝试使用ngForm指令

<form [formGroup]="addfrm" #myform="ngForm">

  <div style="text-align:center">
    <button class="btn btn-primary" (click)="processAdd()" [disabled]="myform.submitted">add</button>
    <button data-dismiss="modal" class="btn btn-default">cancel</button>
  </div>

</form>

You can define a field within your component and set it to true when submitted. 您可以在组件中定义一个字段,并在提交时将其设置为true。

<button class="btn btn-primary" (click)="processAdd()" [disabled]="addfrm.invalid || submitted">add</button>

within component 组件内

export class MyComponent {
    submitted = false;
    ...

    processAdd() {
        this.submitted = true; 
        this.someService.post(this.addForm).subscribe(result => {
            this.submitted = false; // reset it on response from server
        });
    }

}

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

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