如何在 ng2 中重置表单,我已经使用方法 form.reset(),但重置后我仍然选中了表单上的复选框。问题是,如何取消选中表单中的复选框?
<form #f="ngForm" [formGroup]="taskCreate" (submit)="onSubmit(taskCreate)">
<fieldset class="form-group">
<label for="goal">Cel: </label>
<select name="goal" class="form-control" formControlName="goal" ngControl="goal">
<option *ngFor="let goal of goals" [value]="goal.key" [selected]="key === 'first'">{{ goal.value }}</option>
</select>
<div class="alert alert-danger" *ngIf="this.formValid.goalErr">You must select a goal.</div>
</fieldset>
<fieldset class="form-group">
<label for="p">Priorytet:</label>
<div name="p">
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PML" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.minustwo" alt="minustwo" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PL" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.minusone" alt="minusone" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PN" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.zero" alt="zero" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PH" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.plusone" alt="plusone" />
<input class="priority" style="display: table-cell; vertical-align: text-bottom;" type="radio" value="PMH" name="priority" formControlName="priority">
<img style="margin-right: 5px;" [src]="this.plustwo" alt="plustwo" />
</div>
<div class="alert alert-danger" *ngIf="this.formValid.priorityErr">You must select a priority.</div>
</fieldset>
<fieldset class="form-group">
<label for="note">Uwagi do zadania:</label>
<textarea maxlength="2000" class="form-control" name="note" rows="8" cols="40" formControlName="note"></textarea>
<div class="alert alert-danger" *ngIf="this.formValid.noteErr">You must draw a note.</div>
</fieldset>
[...]
TS:
constructor(private appService: AppService, public cartBox: CartBox, public attachments: AttachmentList, private elementRef: ElementRef, private renderer: Renderer) {
super();
this.taskCreate = new FormGroup({
goal: new FormControl("", Validators.required),
prodCodeList: new FormControl("", Validators.required),
note: new FormControl("", Validators.required),
priority: new FormControl("", Validators.required),
attachmentList: new FormControl("")
});
问候! 博斯珀
最佳答案
您可能需要重置每个表单控件。
form.reset() 可以采用由所有表单控件组成的对象。由于您尚未发布如何使用 form.reset,请尝试以下操作。
例如,尝试这样的事情:
this.taskCreate.reset({
goal: '',
prodCodeList: '',
note: '',
priority: '',
attachmentList: ''
})
这应该会重置您拥有的每个表单控件,这意味着它们将保持原样且保持原始状态。
文档 here .
关于javascript - Angular2 表单重置,表单重置或提交后取消选中 radio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615947/