【发布时间】:2013-01-24 11:40:07
【问题描述】:
我的车把模板中有这个:
<span class="currencyFormatMe">{{_current_price}}</span>
循环返回的示例|:出价:24000 美元
我想用逗号格式化它,但我失败了。
我有这个在控制台中工作的功能,但在适应带有把手的代码库时失败。
$.fn.digits = function(){
return this.each(function(){
$(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
})
}
我称之为 $("span.currencyFormatMe").digits();
再次,这一切都在控制台中工作,但在适应时失败。任何指针都非常欢迎
用 registerhelper 尝试过:
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}}
【问题讨论】:
-
你不应该使用 registerHelper
-
@epascarello 更新了上面的代码以反映我对助手的尝试......仍然没有运气。有什么突出的吗?
标签: javascript jquery string-formatting handlebars.js