【问题标题】:jQuery: Convert numeric string in to some standard formatjQuery:将数字字符串转换为某种标准格式
【发布时间】:2012-07-11 09:28:11
【问题描述】:

是否有任何 jQuery 插件可以将数字字符串转换为标准格式:

200000 to 2,00,000
1000 to 1,000
398740 to 3,987,40

等等..

【问题讨论】:

标签: jquery


【解决方案1】:

试试这个:

var s = 200000;
var converted = s.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")

DEMO

或:

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

你可以调用它来像其他jQuery方法一样转换选定元素的文本:

$('#elem').convert()

【讨论】:

  • 如果我将此功能与页面刷新一起使用很好,但如果我将它与 AJAX 发送的响应一起使用 POST firesbug 显示正确的 HTML 232343 和函数也在那里,但不格式化数字字符串。我认为 jQuery live() 可以使用,但我不知道。
  • 好吧,我只是绑定
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 2012-01-26
  • 2013-07-11
相关资源
最近更新 更多