最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ANGULAR - Disable submit button if form value not changed - Stack Overflow

programmeradmin3浏览0评论

i have form for selected date. how to disable the submit button if nothing changed in date form?

my html code

<div class="col-xs-6">
   <label><i class="far fa-calendar-alt"></i> Start <span class="text-danger">*</span></label>
     <input id="startTrip1" name="NgbDate" data-provide="datepicker" ngbDatepicker #d="ngbDatepicker" [markDisabled]="markDisabled" [minDate]="minDates" type="text" class="form-control form-flat" [(ngModel)]="ad.start_date" (dateSelect)="onDateSelect($event, ad)" (blur)="validateInput()" (click)="d.toggle()" [ngModelOptions]="{standalone: true}" [disabled]="form.controls.tripduration.hasError('required')" >
      <div class="text-danger" *ngIf="(ad.start_date == '' || ad.start_date == undefined) && ngForm.submitted">
         * This field is required
       </div>
      <div class="text-danger" *ngIf="form.controls.tripduration.hasError('required')">
      * Fill the durations first
      </div>
 </div>
//submit button
<button class="col-xs-12 text-center text-strong pointer custom-trip-btn" (click)="publishTrip()">
<button class="custom-trip-btn">SUBMIT</button>

i have form for selected date. how to disable the submit button if nothing changed in date form?

my html code

<div class="col-xs-6">
   <label><i class="far fa-calendar-alt"></i> Start <span class="text-danger">*</span></label>
     <input id="startTrip1" name="NgbDate" data-provide="datepicker" ngbDatepicker #d="ngbDatepicker" [markDisabled]="markDisabled" [minDate]="minDates" type="text" class="form-control form-flat" [(ngModel)]="ad.start_date" (dateSelect)="onDateSelect($event, ad)" (blur)="validateInput()" (click)="d.toggle()" [ngModelOptions]="{standalone: true}" [disabled]="form.controls.tripduration.hasError('required')" >
      <div class="text-danger" *ngIf="(ad.start_date == '' || ad.start_date == undefined) && ngForm.submitted">
         * This field is required
       </div>
      <div class="text-danger" *ngIf="form.controls.tripduration.hasError('required')">
      * Fill the durations first
      </div>
 </div>
//submit button
<button class="col-xs-12 text-center text-strong pointer custom-trip-btn" (click)="publishTrip()">
<button class="custom-trip-btn">SUBMIT</button>
Share Improve this question edited Feb 26, 2019 at 10:27 Sivaramakrishnan 7391 gold badge4 silver badges11 bronze badges asked Feb 26, 2019 at 8:25 Soni SilalahiSoni Silalahi 2492 gold badges5 silver badges15 bronze badges 2
  • Do you only want this when the page is loaded or also when the user changes a value from the original to something else and then back again? – Markus Deibel Commented Feb 26, 2019 at 8:27
  • when the user changes a value from the original to something else and then back again – Soni Silalahi Commented Feb 26, 2019 at 8:34
Add a comment  | 

3 Answers 3

Reset to default 12

If you used below code, the button only enabled if user changed value in the form

[disabled]="!(accountForm.valid && accountForm.dirty)"

<form [formGroup]="profileForm">

  <label>
    First Name:
    <input type="text" formControlName="firstName">
  </label>

  <label>
    Last Name:
    <input type="text" formControlName="lastName">
  </label>

</form>    

<button type="submit" [disabled]="!profileForm.dirty">Submit</button>

Read more about it here

Hope this works!

  <label>
    First Name:
    <input type="text" formControlName="firstName">
  </label>

  <label>
    Last Name:
    <input type="text" formControlName="lastName">
  </label>

</form>    

<button [disabled]="!(profileForm.valid ||!profileForm['isDirty']())" 
class="btn btn-primary">Submit</button>

Check some what same question here

Stackblitz-example of some old problem of mine

for the above you need to use RxReactiveFormsModule RxReactive npm

发布评论

评论列表(0)

  1. 暂无评论