我有一个表单,其中一个字段充当自动完成功能。如果用户输入单词并按Enter键,则该字段的内容应添加到该字段下方的列表中。
问题:当用户点击进入时,自然会提交整个表单。
我已经return false
处理了按键的功能。但是,即使在调用此函数之前,表单似乎也已提交。
如何防止这种情况发生?
基本形式:
<div id="profileForm">
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()" method="post" *ngIf="!showSuccessMessage">
<div class="row">
<div class="form-group col-xs-12 col-sm-6">
<label for="first_name">My Skills</label>
<div class="autocomplete">
<input formControlName="skill_string" [(ngModel)]="skillString" name="skill_string"
type="text" class="form-control" id="skill_string" placeholder="Comma separated" (keyup.enter)="skillsHandleEnter(skillString)">
<ul class="autocomplete-list" *ngIf="skillHints.length > 0">
<li class="list-item" *ngFor="let skill of skillHints" (click)="addSkillFromAutocomplete(skill)">{{skill}}</li>
</ul>
</div>
<div id="skill-cloud" class="tag-cloud">
<span class="skill-tag tag label label-success" *ngFor="let skill of selectedSkills" (click)="removeSkill(skill)">{{skill}} x</span>
</div>
</div>
</div>
<div class="row">
<hr>
<div class="form-group submit-group">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary pull-right" [disabled]="!profileForm.valid">Save</button>
</div>
</div>
</div>
</form>
</div>
基本组件(我在这里发布了很多用于发布它的逻辑):
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs/Rx';
import 'rxjs/add/operator/debounceTime';
import * as _ from 'lodash';
import { MemberService } from '../shared/index';
@Component({
moduleId: module.id,
selector: 'signup',
templateUrl: 'signup.component.html',
styleUrls: ['signup.component.css']
})
export class SignupComponent implements OnInit {
private profileForm:FormGroup;
private validation_errors:Array<any>;
private selectedSkills:Array<string>;
private skillHints:Array<string>;
private skillString:string;
constructor(private route: ActivatedRoute,
private formBuilder: FormBuilder,
private memberService: MemberService,
private router: Router ) {
this.selectedSkills = [];
this.skillHints = [];
this.skillString = '';
// Set up form
this.profileForm = this.formBuilder.group({
skill_string: ['']
});
}
ngOnInit(): any {
// Do something
}
updateSelectedSkills(skillString:string):void {
if(skillString) ) {
let cleanString = skillString.trim().replace(/[ ]{2,}/g, ' ');
this.selectedSkills = _.compact(this.selectedSkills.concat(cleanString.split(',')));
this.skillString = '';
this.skillHints = [];
}
}
skillsHandleEnter(skillString:string):void {
console.log("ENTER");
this.updateSelectedSkills(skillString);
return false;
}
autocompleteSkills(term:string):void {
this.memberService.autocompleteSkills(term).subscribe(
res => {
this.skillHints = [];
for(let i = 0; i < res.data.length; i++) {
this.skillHints.push(res.data[i].name);
}
}
);
}
addSkillFromAutocomplete(skillString:string):void {
this.selectedSkills.push(skillString);
this.memberProfile.skill_string = '';
this.skillHints = [];
this.skillString = '';
}
onSubmit():void {
this.memberService.saveProfile(this.memberProfile, this.selectedSkills).subscribe(
res => {
console.log(res);
}
);
}
}
答案 0 :(得分:13)
尝试
<form (keydown.enter)="$event.target.tagName == 'TEXTAREA'" [formGroup]="profileForm" (ngSubmit)="onSubmit($event)">
它还允许enter
中的Textarea
。
答案 1 :(得分:6)
所以答案实际上很简单......因为我在输入字段而不是按钮上听Enter,所以它不是Event.preventDefault()
。从type="submit"
删除button
是不够的,因为默认情况下所有按钮都是提交类型。唯一需要做的更改是在按钮元素上,明确添加type="button"
并添加(click)
侦听器:
<button type="button" (click)="onSubmit()" class="btn btn-primary pull-right" [disabled]="!profileForm.valid">Save</button>
唯一的问题:现在提交带有输入的表单永远不会有效。如果焦点位于自动完成输入字段中,则只会阻止输入提交表单。
修改强>
要仅在光标处于自动完成字段时阻止输入提交表单,可以通过使用Ankit Singh的解决方案并稍微修改它来实现:
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()" method="post" (keydown.enter)="$event.target.id != 'skill_string'" *ngIf="!showSuccessMessage">
(注意:条件必须返回false以防止触发默认操作)
当然我们再次需要我们的常规提交按钮(没有附加点击事件,或者表格会提交两次):
<button type="submit" class="btn btn-primary pull-right" [disabled]="!profileForm.valid">Save</button>
如果您想使用event.target.classList
课程,也可以查看.autocomplete
。或者将检查逻辑移动到您传递$event
。
答案 2 :(得分:2)
Angular 2中的事件表现得像普通的DOM事件。要捕获事件对象,请在模板的事件回调中将$event
作为参数传递:
HTML:
<button (keyup.enter)="skillsHandleEnter($event, skillString)"></button>
使用Event.preventDefault()的JavaScript:
@Component(...)
class MyComponent {
skillsHandleEnter(event, skillString) {
event.preventDefault();
// ... your logic
}
}
答案 3 :(得分:0)
阻止表单在 enter 上提交或单击button
<textarea (keydown.enter)="provoked($event)"></textarea>
<button (keydown.enter)="provoked($event)" (click)="provoked($event)"></button>
provoked($event) {
$event.preventDefault()
}
如果需要将数据传递给该方法,请尝试
<textarea (keydown.enter)="provoked($event, data)"></textarea>
<button (keydown.enter)="provoked($event, data)" (click)="provoked($event, data)"></button>
provoked($event, data) {
$event.preventDefault()
// process the data here
}