在输入类型编号中添加千位和小数分隔符

时间:2012-10-14 10:42:36

标签: javascript jquery jeditable separator

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

我正在为项目使用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

你能帮我解决一下如何正确添加十进制和千位分隔符吗?

由于

2 个答案:

答案 0 :(得分:0)

你可以尝试这个:

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(''),' ','.'));
});​

答案 1 :(得分:0)

像这样?

    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(''),' ','.'));
});​