【问题标题】:Replace a dot with a comma inside a jQuery template在 jQuery 模板中用逗号替换点
【发布时间】:2014-11-08 10:31:40
【问题描述】:

你有一个非常简单的 jQuery 模板,我说如下:

<dt>${Math.round(ShippingCost*100)/100} kr.</dt>

但是,这会产生一个值,例如 200.00 kr。

现在我想用逗号替换我的点,我这样做:

<dt>${(Math.round(ShippingCost*100)/100).replace(".",",")} kr.</dt>

这会导致:

Uncaught SyntaxError: Unexpected token { 

知道怎么解决吗?

【问题讨论】:

  • (Math.round(ShippingCost*100)/100).toString().replace(".",",")
  • @dfsq 导致同样的错误,不幸的是:-(

标签: jquery jquery-templates


【解决方案1】:

${} 中的表达式用括号括起来:

${((Math.round(ShippingCost*100)/100).toString().replace(".",","))}

如果没有它,模板引擎似乎会感到困惑。

您需要解决的另一件事是您必须将Math.round 的结果转换为字符串类型,因为replace 是字符串的方法,而不是数字。您可以为此使用简单的toString()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多