【问题标题】:Adding thousand and decimal separator in input type number在输入类型数字中添加千位和小数分隔符
【发布时间】:2012-10-14 10:42:36
【问题描述】:

我做了很长时间的研究,但我没有找到类似的案例。

我正在为一个项目使用jeditable 插件,我需要创建一个输入类型号

首先我在 jquery.jeditable.js 中创建了输入类型号

    number: {
        element : function(settings, original) {
            var input = $('<input type="number" step="0.00">');
            if (settings.width  != 'none') { input.attr('width', settings.width);  }
            if (settings.height != 'none') { input.attr('height', settings.height); }
            /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
            //input[0].setAttribute('autocomplete','off');
            input.attr('number','off');
            $(this).append(input);
            return(input);
        }
    },

然后我将脚本放入我的 html 文件中

    $('.number').editable(function(value, settings) { 
     console.log(this);
     console.log(value);
     console.log(settings);
     return(value);
  }, { 
     type    : 'number', 
     style  : "inherit"
 });

我的html

<p class="number" style="text-align: right;">000,00</p>

问题是我不知道如何在所有浏览器中使用十进制和千位分隔符。正如您在上面看到的,我刚刚放了 step="0.00 但这仅适用于 FF

您能帮我了解如何正确添加小数和千位分隔符吗?

谢谢

【问题讨论】:

    标签: javascript jquery jeditable separator


    【解决方案1】:

    你可以试试这个:

    function format(comma, period) {
    comma = comma || ',';
    period = period || '.';
    var split = this.toString().split('.');
    var numeric = split[0];
    var decimal = split.length > 1 ? period + split[1] : '';
    var reg = /(\d+)(\d{3})/;
        while (reg.test(numeric)) {
        numeric = numeric.replace(reg, '$1' + comma + '$2');
        }
    return numeric + decimal;}
    
    $('#mydiv').live('keyup', function(){
    $(this).val(format.call($(this).val().split(' ').join(''),' ','.'));
    });​
    

    【讨论】:

    • 我正在尝试,但实际上我真的不知道为什么 jeditable 插件不响应它。我认为这是因为 jeditable 将文本转换为表单然后返回...
    • 好的,你现在怎么解决这个问题?我迷路了,卡住了,非常欢迎所有的建议和建议
    • 编辑:好的,“step”参数似乎引起了麻烦。我只是删除该参数,让我的示例代码在 jeditable 处理它们之后处理前端中的数字。因此,只需制作另一个/编辑您现有的 .js 文件并使用您的选择器而不是我的“#mydiv”。
    • 这可能行得通,是的。但是为什么不单独处理你的 jeditable 输出呢?只需创建另一个 *new .js 文件,然后让我的代码示例重新访问您的“.number”类上的 jeditable 输出?!
    • 因为我的 jquery.jeditable.js 中还有其他输入类型,所以我在编辑答案中的 che 代码时可能会犯一些错误
    【解决方案2】:

    像这样?

        number: {
            element : function format(comma, period) {
                comma = comma || ',';
                period = period || '.';
                var split = this.toString().split('.');
                var numeric = split[0];
                var decimal = split.length > 1 ? period + split[1] : '';
                var reg = /(\d+)(\d{3})/;
                    while (reg.test(numeric)) {
                        numeric = numeric.replace(reg, '$1' + comma + '$2');
                        }
                    return numeric + decimal;}
                var input = $('<input type="number"');
                if (settings.width  != 'none') { input.attr('width', settings.width);  }
                if (settings.height != 'none') { input.attr('height', settings.height); }
                /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
                //input[0].setAttribute('autocomplete','off');
                input.attr('number','off');
                $(this).append(input);
                return(input);
            }
        },
    
    
    
    $('.number').live('keyup', function(){
    $(this).val(format.call($(this).val().split(' ').join(''),' ','.'));
    });​
    

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2013-09-02
      • 2022-12-03
      相关资源
      最近更新 更多