单击内部表单时,即使必填字段为空,也会以角度形式提交



当它的值发生变化时,形式上有下拉菜单,我检查它的下拉列表是否下拉。如果下拉列表,则用户必须输入下拉列表的值。在下拉列表的更改中,我调用"ChangeSortOrder"功能,但随之而来的是表单被提交。因此,我过去常常单击提交按钮,而不是NgSubmit。但现在的问题是,即使值为空,它也不会检查必填字段并提交。

<form role="form" class="form form-horizontal" #Editform="ngForm" ngNativeValidate>
<div ngbDropdown class="col-md-6" style="float:left">
<button class="btn btn-outline-allow" id="dropdownBasic1" ngbDropdownToggle>{{selectedQuestionType}}</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button class="dropdown-item" (click)="ChangeSortOrder('Text')">Text</button>
<button class="dropdown-item" (click)="ChangeSortOrder('DropDown')">DropDown</button>
</div>
<ul class="list-group ">
<li class="list-group-item" *ngIf="selectedQuestionType == 'DropDown'">
You have selected a Drop Down question. This means diner will select from 5 options in response to this question. This score can contribute to your overall score and section.
</li>
<li class="list-group-item" *ngIf="selectedQuestionType == 'Text'">
You have selected a text question. This means the diner will enter a text in response to this question. This score can contribute to your overall score and section.
</li>
</ul>
<!-- Question -->
<div class="form-group mt-2">
<div class="position-relative has-icon-left">
<textarea class="form-control" name="question" value={{editQuestion.question}} [(ngModel)]="question" required> </textarea>
<div class="form-control-position">
<i class="fa fa-question allow"></i>
</div>
</div>
</div>
<!-- DropDown -->
<div class="position-relative has-icon-left" *ngIf="selectedQuestionType == 'DropDown'">
<input type="text" class="form-control" name="DropDown1" [(ngModel)]="DropDown1" placeholder="Give Label for drop down" required/>
<div class="form-control-position">
<i class="fa fa-angle-double-right success"></i>
</div>
</div>

<div class="position-relative has-icon-left" *ngIf="selectedQuestionType == 'DropDown'">
<input type="text" class="form-control" name="DropDown2" [(ngModel)]="DropDown2" placeholder="Give Label for drop down" required/>
<div class="form-control-position">
<i class="fa fa-angle-double-right" style="color:#7CB342"></i>
</div>
</div>
</div>
<div class="pull-right col-md-6">
<div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" [(ngModel)]="modelRadio">
<label ngbButtonLabel class="btn-raised btn-outline-allow">
<input ngbButton type="radio" [value]="2"> Staff
</label>
<label ngbButtonLabel class="btn-raised btn-outline-allow">
<input ngbButton type="radio" [value]="3"> Marketing
</label>
</div>
<ul class="list-group">
<li class="list-group-item" *ngIf="modelRadio == 2">
You have selected a Staff question. We will provide you with insights on where are you performing well and where there is
room for improvement.
</li>
<li class="list-group-item" *ngIf="modelRadio == 3">
You have selected a Marketing question. We will provide you with insights on where are you performing well and where there is room for improvement.
</li>
</ul>
</div>
<button type="submit" (click)="onSubmit()" class="btn btn-raised btn-danger pull-right mt-2">Save</button>
</form>

有几种方法可以防止提交表单:

  1. 将按钮的类型设置为type="button"

  2. 在按钮上调用$event.preventDefault();

<button class="dropdown-item" (click)="ChangeSortOrder('DropDown'); $event.preventDefault()">DropDown</button>

您是否尝试过将按钮类型从"提交"更改为"按钮"?

<button type="button" (click)="onSubmit()" class="btn btn-raised btn-danger pull-right mt-2"> Save </button>

最新更新