【问题标题】:How to append and prepend a character on the mat input?如何在垫子输入上附加和前置一个字符?
【发布时间】:2020-11-19 08:17:45
【问题描述】:

我的 html 文件为

<div class="space-between-items">
     <mat-form-field>
        <mat-label>NEC LEC</mat-label>
        <input #neclec matInput formControlName="necLec"  autocomplete="off" 
           [minLength]="NEC_LEC_MIN_LENGTH" [maxLength]="NEC_LEC_MAX_LENGTH"
           [value]="neclec.value.toString().toUpperCase()">
     </mat-form-field>
     <mat-checkbox color="primary" formControlName="chkNecLec"></mat-checkbox>
  </div>

当我保存在此字段中输入内容时,它会更新网格,就像现在一样。我如何让它自动将大括号添加到我的输入中。

我希望它始终与大括号一起保存,而不必在输入端手动输入大括号,这样当我输入“REC”时,它会在网格中显示为 (REC)。

【问题讨论】:

    标签: angular mat-input


    【解决方案1】:

    以下解决方案可能适合您。

    function myfunc(value){
        if(!value.startsWith("(")){
        value = "(" + value;
      }
      if(!value.endsWith(")")){
        value = value + ")"
      }
      document.getElementById("abc").value = value;
    }
    &lt;input id="abc" type="text" onKeyUp="myfunc(value)" value="()"/&gt;

    【讨论】:

    • 谢谢@Shabbir Dhangot。我不得不从 (onKeyUp) 更改为 (change) 并且效果很好。
    猜你喜欢
    • 1970-01-01
    • 2020-04-12
    • 2017-04-27
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多