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

时间:2018-08-29 06:46:18

标签: angular angular6 form-submit angular-forms

当其值更改时,表单中会出现一个下拉菜单,我会检查其是否下拉列表。如果其下拉菜单,则用户必须输入下拉菜单的值。在下拉菜单的更改中,我调用“ ChangeSortOrder”函数,但与此同时,表单也被提交。所以我不是点击NgSubmit而是点击Submit按钮。但是现在的问题是,即使值是空的,也不会检查必填字段并提交。

<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>

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

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

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