Javascript 删除Jquery中字符串中的所有逗号

Javascript 删除Jquery中字符串中的所有逗号,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有很多类,如果其中存在逗号,我想删除其中的逗号 我写了下面的代码,但代码不能正常工作。第二个类值将替换为第一个值 var removebox=$(“.remove”), removeboxHtml=removebox.html(); removebox.html(removeboxHtml.replace(/,/g') ,17500000 ,2479000 我将迭代每个元素并更改其“文本: var removebox = $(".remove"); removebox.each(fun

我有很多类,如果其中存在逗号,我想删除其中的逗号

我写了下面的代码,但代码不能正常工作。第二个类值将替换为第一个值


var removebox=$(“.remove”),
removeboxHtml=removebox.html();
removebox.html(removeboxHtml.replace(/,/g')

,17500000
,2479000

我将迭代每个元素并更改其“文本:

var removebox = $(".remove");

removebox.each(function () {

  var oldtext = $(this).text();

  $(this).text(oldtext.replace(',', ''));

});

试试这个。更新了您的代码:


$(“.remove”).each(函数(){
$(this.html($(this.html().replace(/,/g,));
});

,17500000
,2479000

虽然您已经接受了这个问题的答案,但值得指出的是,它们都太冗长了,一个答案(在撰写本文时)只会从每个给定元素中删除一个逗号(如果其中存在多个逗号)

也就是说,更简明的版本如下:

// select the elements to update:
$('.remove')

  // we then use the text method's anonymous function,
  // using the two arguments:
  // index: the index of the current element in the collection,
  // currentText: the text of the current element of the
  // collection over the text method iterates:
  .text(function(index, currentText) {

    // here we access the currentText variable and use
    // String.prototype.replace(), with a regular literal (/.../)
    // to remove all occurrences (g) of the specified comma character
    // replacing those occurrences with an empty string (''); this
    // is comma-removed string is returned to the text method in
    // order to update the text of each element as required:
    return currentText.replace(/,/g, '');
});
$('.remove').text(函数(索引,currentText){
返回currentText.replace(/,/g',);
});

,17500000
,2479000

5279000
但代码不能正常工作
-问题到底是什么,说它不能工作
并没有切断它我运行了您提供的代码段,它运行正常。@trincot是的,它可以工作,但第二个类值替换为第一个类值value@inaz,那么你为什么不在问题中提及这一点呢?似乎是重要信息。@trincot-他确实说过:“代码工作不正常”-这应该足够了:请将
removeboxHtml
设置为本地变量,并且不要污染全局命名空间。已删除
removeboxHtml
variable@AlivetoDie很乐意帮忙。