javascript - 如何在表单标签内添加点击事件

标签 javascript angular typescript angular-forms

我正在使用 ionic 在 typescript 中创建 angular2 形式,

.html

<form (ngSubmit)="verify(form)" #form="ngForm">
<ion-input type="text" name="data" #number="ngModel" maxlength='4" [(ngModel)]="digits"></ion-input>
<span *ngIf="number.dirty && form.submitted && form.value.number<4">Enter all numbers</span>
<span *ngIf="number.pristine && form.submitted">Enter number</span>

<button (click)="doAgain()">Click Again</button>

<button type="submit">Verify</button>
</form>

问题1:当我单击“再次单击”按钮时,错误消息显示为“输入数字”,我不想显示它也会触发 verify() 函数。

更新: 问题2:我的要求是我只需要在文本框中输入数字,而不需要输入字母和特殊字符,例如'a','b','@'。因此,通过上述实现,输入框接收 b c d 和 @ # $ 和 1 2 3 任何内容。我想将其限制为仅数字。我知道,因为我已将属性提供为 type="text",所以它会以这种方式运行。当我更改为 type="number"时,我实现了目标,但输入字段接受的字符数超过了我的 maxlength,即 4。

最佳答案

您应该添加按钮的类型

<button (click)="doAgain()" type='button'>Click Again</button>

因为 ngSubmit 将在单击表单组内的任何按钮时触发。 为了区分submit按钮和其他按钮,我们使用这个type

<强> Read here for more information on it

关于javascript - 如何在表单标签内添加点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353256/

相关文章:

typescript - 枚举的静态方法

angular - 如何解决 Angular 单元测试错误 : "An error was thrown in afterAll\n[object ErrorEvent] thrown"

typescript - tsx 找不到模块 'xxx'

javascript - 如何从 vuex 中查看存储值?

javascript - 根据位置重定向某些网址

javascript - Intl.NumberFormat 以挪威克朗格式化价格

Angular 6 Material Nested Tree 不适用于动态数据

angular - 为什么在 typescript 的 const 中使用带有 ${ 的反勾

javascript - 从数据库获取数据时html中的多个选择选项

angular - 更新 TSLint 错误 : Could not find implementations for the following rules specified in the configuration