首页 > 解决方案 > 我想创建只允许数字的文本框。我如何在 aurelia 中使用 Typescript 来做到这一点?

问题描述

我只想在文本框中输入数字,所以我添加了 oninput,但我想使用 bindableproperty 创建。我不知道该怎么做。请建议我?

文本框.html

<template bindable="">
  <input style="
         box-sizing: border-box;"
         maxlength="10" ;
         oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" />
</template>;   

文本框.ts

import { bindable } from 'aurelia-framework';
export class textbox {
  constructor() {
  }
}

应用程序.html

<template bindable="">
  <require from="./components/textbox.html"></require>
  <textbox></textbox>
</template>

应用程序.ts

export class App {
}

标签: typescripttextboxaurelia

解决方案


I am just giving the idea, might be helpful:

In Html you may use  
(keyup)="methodName($event.targer.value)"  event.


In you ts file you may use:
bindedTextareaProperty;

methodName(event) {
if(isNumber(event){

} else {
// you may the value of last index
bindedTextareaProperty.slice(0, bindedTextareaProperty.length -1);
}
}

推荐阅读