【问题标题】:Latest Autonumeric is allowing decimal point even if decimalPlaces set to 0即使 decimalPlaces 设置为 0,最新的 Autonumeric 也允许小数点
【发布时间】:2019-06-22 14:26:54
【问题描述】:

我要求输入文本框允许整数,我确实要求货币符号和逗号。我使用了最新版本的自动数字 js。

我已将 decimalPlaces 属性设置为 0,但它仍然允许我按一次该点,并在进一步按键时删除该点。如果decimalPlace属性设置为0,我想让点不被压在第一位

下面是片段和 JS 小提琴链接。寻求任何形式的帮助

AutoNumeric.multiple('.testInput', 
{ currencySymbol: '$', decimalPlaces: 0, unformatOnSubmit: true, modifyValueOnWheel: false });

http://jsfiddle.net/wpomn0d2/

【问题讨论】:

    标签: javascript jquery asp.net-mvc autonumber


    【解决方案1】:

    您可以使用getPredefinedOptions() 函数获取decimalPlaces 值并指定integer.decimalPlaces 属性:

    var places = AutoNumeric.getPredefinedOptions().integer.decimalPlaces;
    

    然后使用它的值来防止在keydown事件上插入非数字符号:

    $('.testInput').keydown(function (e) {
        var places = AutoNumeric.getPredefinedOptions().integer.decimalPlaces;
    
        if (places == 0 && (e.which < 48 || e.which > 57))
        {
            // prevent symbol insertion
            return false;
        }
    
        // other stuff
    });
    

    JSFiddle example

    参考:AutoNumeric static methods

    【讨论】:

      猜你喜欢
      • 2010-12-25
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2012-03-24
      相关资源
      最近更新 更多