Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript设置货币格式的Handlebar函数_Javascript_Jquery_String Formatting_Handlebars.js - Fatal编程技术网

使用javascript设置货币格式的Handlebar函数

使用javascript设置货币格式的Handlebar函数,javascript,jquery,string-formatting,handlebars.js,Javascript,Jquery,String Formatting,Handlebars.js,我的车把模板中有: <span class="currencyFormatMe">{{_current_price}}</span> 我把它叫做 $(“span.currencyFormatMe”).digits() 同样,这一切都在控制台中工作,但在适应时失败。欢迎任何指点 使用注册表帮助器进行了尝试: Handlebars.registerHelper('formatCurrency', $.fn.digits = function(){

我的车把模板中有:

<span class="currencyFormatMe">{{_current_price}}</span>
我把它叫做 $(“span.currencyFormatMe”).digits()

同样,这一切都在控制台中工作,但在适应时失败。欢迎任何指点

使用注册表帮助器进行了尝试:

Handlebars.registerHelper('formatCurrency',
    $.fn.digits = function(){ 
        return this.each(function(){ 
            $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
        })
    }
);
电话:

{{formatCurrency _current_price}}

这里有几个简单的选项

您可以坚持使用jQuery插件,并在填充Handlebar模板后应用它;大概是这样的:

<script id="t" type="text/x-handlebars">
    <span class="currencyFormatMe">{{_current_price}}</span>
</script>
然后:

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
};

var t = Handlebars.compile($('#t').html());
var h = t({
    _current_price: 1000
});
$('<div>').append(h).find('.currencyFormatMe').digits();
Handlebars.registerHelper('formatCurrency', function(value) {
    return value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
});

var t = Handlebars.compile($('#t').html());
var h = t({
    _current_price: 1000
});

演示:

您不应该使用registerHelper@epascarello更新了上面的代码以反映我在帮助者方面的尝试…仍然没有运气。有什么突出的地方吗?谢谢上面的内容,但它不适用于24000.50美元的出价。关于那部分有什么建议吗?@tahirabayashi你是说
.50
部分?可能最好查找
prinft
JavaScript库,并将其包装在Handlebar助手中
<script id="t" type="text/x-handlebars">
    {{formatCurrency _current_price}}
</script>
Handlebars.registerHelper('formatCurrency', function(value) {
    return value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
});

var t = Handlebars.compile($('#t').html());
var h = t({
    _current_price: 1000
});