【问题标题】:Using Array in ng-model for a input of type number shows default value as 0在 ng-model 中使用 Array 输入类型 number 将默认值显示为 0
【发布时间】:2018-01-20 08:06:58
【问题描述】:

我有一个数字类型的输入框,我在其中使用了 ng-model,它是一个数组,当数组为空时,输入将值显示为 0,但是当我输入数字时,现有的零不会被删除,并且它显示以 0 为起始的值。

Eg:   Instead of 5 it shows as 05

HTML:

<input stopccp decimalpoint
       type="number"
       placeholder="{{'ededuction_message' | translate}} - {{ vm.singleproduct.selectedtype === 'piece' ? 'Pec' : 'Kg' }}"
       ng-model="vm.total_amount[$index]"
       id="field2"
       >

控制器:

vm.total_amount = [];

我怎么能只显示我输入的值而不是显示零作为前缀。

【问题讨论】:

    标签: javascript arrays angularjs ionic-framework


    【解决方案1】:

    在 onkeydown 中使用字符串替换:

    onkeydown="if(this.value.length !== 0) this.value = this.value.replace('0','');"
    

    这个正在替换你所有输入的 0 值

    onkeydown="if(this.value.length !== 0) this.value = this.value.indexOf('0') == 0 ? this.value.substring(1) : this.value;"
    

    这个正在替换您的特定索引值

    【讨论】:

    • 让我检查一下
    【解决方案2】:

    你也可以这样做:

    onchange="this.value=this.value*1"
    

    这将自动将输入的值转换为数字。

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2016-11-18
      • 2018-03-10
      • 1970-01-01
      • 2021-04-27
      • 2018-09-28
      • 2014-01-11
      • 2016-04-27
      • 1970-01-01
      相关资源
      最近更新 更多