javascript 使用Javascript删除值的逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7842875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:31:10  来源:igfitidea点击:

Removing comma for value with Javascript

javascript

提问by XML guy

thanks for looking my questions, I can't find the way to remove the comma with javascript. So I have problem at removing comma from page with java-script, Adding Comma for output is fine i guess, but for removing comma for value, the function is not working for me.

感谢您查看我的问题,我找不到用 javascript 删除逗号的方法。因此,我在使用 java-script 从页面中删除逗号时遇到问题,我想为输出添加逗号很好,但是对于删除值的逗号,该函数对我不起作用。

The source code is : http://jsfiddle.net/Q5CwM/2/

源代码是:http: //jsfiddle.net/Q5CwM/2/

Please let me know, thanks again.

请告知,再次感谢。

回答by jfriend00

I have no idea what your code is trying to do overall, but you can fix this function that removes commas:

我不知道你的代码总体上想做什么,但你可以修复这个删除逗号的函数:

function checkNumeric(objName) {
    var lstLetters = objName;
    var lstReplace = lstLetters.replace(/\,/g,'');
}

by changing it to this:

通过将其更改为:

function removeCommas(str) {
    return(str.replace(/,/g,''));
}

You weren't returning the changed string from the function and I changed the name of the function and parameters to represent what it does.

您没有从函数返回更改后的字符串,我更改了函数和参数的名称以表示它的作用。

str.replacereturns the changed string. It does not change the string you started with so in order to do something with the result of the replacement, you have to either return that from the function or assign it to some other string. As you had it, nothing was happening with the replaced string so the function did nothing.

str.replace返回更改后的字符串。它不会更改您开始使用的字符串,因此为了对替换结果执行某些操作,您必须从函数返回该字符串或将其分配给其他字符串。正如您所拥有的,替换后的字符串没有发生任何事情,因此该函数什么也没做。