簡體   English   中英

我想在文本框中自動添加破折號 (-)。 如何在 aurelia typescript 應用程序中執行此操作?

[英]I want to automatically add dashes (-) in a textbox. How can I do this in an aurelia typescript application?

在這里,我正在嘗試創建一個文本框,該文本框將允許 XXX-XXX-XXXX 格式的聯系號碼。 但我無法創建這個。 如何在 typescript aurelia 中創建格式化的聯系號碼?

文本框.html

<input type="text" name.bind="name" class="form-control" placeholder.bind="placeholder" maxlength.bind="maxlength" onblur="addHyphen(f)"/>

文本框.ts

addHyphen(f){
    f.value = f.value.slice(0,3)+"-"+f.value.slice(3,6)+"-"+f.value.slice(6,10);
   }

我對此進行了更新,使其類似於您在評論中提供的 JsFiddle/

文本框.html

<template>
  <input type="text" name.bind="name" class="form-control" 
    placeholder.bind="placeholder" maxlength.bind="maxlength
    keydown.delegate="keydown($event)" ref="text"/>
</template>

文本框.ts

export class Textbox {
  public text: HTMLInputElement;

  public keydown(e) {
      const key = e.key || e.keyCode || 0;
      if (key !== 8 && key !== 9) {
          if (this.text.value.length === 3) {
              this.text.value += '-';
              console.log(e);
          }
          if (this.text.value.length === 7) {
              this.text.value += '-';
              console.log(e);
          }
      }
    return (key == 8 || key == 9 || key == 46 || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM