php 使用 Jquery 删除逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7802014/
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
Remove Commas with Jquery
提问by Devyn
I am in need of some code that removes commas from a string. I currently have a variety of numbers in the number_format() for PHP. I use Jquery to post certain things to a update page and I need the commas removed from a class.
我需要一些从字符串中删除逗号的代码。我目前在 PHP 的 number_format() 中有各种数字。我使用 Jquery 将某些内容发布到更新页面,我需要从类中删除逗号。
for instance here is some code.
例如这里是一些代码。
<span class="money">1,234,567</span>
I want the value of the code I post to be 1234567 instead of 1,234,567. I would like to use Jquery if that's possible.
我希望我发布的代码的值是 1234567 而不是 1,234,567。如果可能的话,我想使用 Jquery。
Thanks
谢谢
回答by Peyu_Atrellu
This is a clean way to get rid of the commas in one line, without loops.
这是一种摆脱一行中逗号的干净方法,没有循环。
var cleanNumber = $("#selector").val().split(",").join("");
Hope can help u!
希望能帮到你!
回答by Kevin Redman
Less is more! (sometimes!)
少即是多!(有时!)
$(document).ready(function(){
var **stripString** = $('.money').text().replace(/,/g, '');
$('.*money*').text(**stripString**);
});
回答by Jayantha Lal Sirisena
you can do like this
你可以这样做
var str=$(".money").text();
var str2= str.replace(",", "")
回答by g.m.ashaduzzaman
Could you please try with this , will remove comma on click on textbox , after focusout thousand seperated will be added...
你能不能试试这个,点击文本框时会删除逗号,在焦点千分后将被添加......
$( "#salary" ).click(function() {
$( "#salary" ).val( $("#salary").val().replace(/,/g, ''));
});
$( "#salary" ).blur(function() {
$( "#salary" ).val( addCommas($( "#salary" ).val());
});
function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '' + ',' + '');
}
return x1 + x2;
}
回答by Gaurav Agrawal
use replace function of javascript for removing , in a string
使用 javascript 的替换功能删除字符串中的 ,
var result = jQuery('.money').html().replace(',', '');
alert(result);