Javascript 从价格中去除美元符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4209130/
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-08-23 11:23:02  来源:igfitidea点击:

Removing dollar signs from prices

javascriptregexstring

提问by Mike Muller

I'm building a string of amounts but need to remove the dollar signs. I have this jQuery code:

我正在建立一串金额,但需要删除美元符号。我有这个 jQuery 代码:

  buildList($('.productPriceID > .productitemcell'), 'pricelist')

It's returning

它回来了

pricelist=.00,.50,.50

I need to remove the dollar signs but can't seem to figure it out. Tried using .trim but I think that removes only white space.

我需要删除美元符号,但似乎无法弄清楚。尝试使用 .trim 但我认为这只会删除空白。

Sorry for the newbie question! Thanks in advance for any help!

对不起,新手问题!在此先感谢您的帮助!

Here's the full code:

这是完整的代码:

function buildList(items, name) {
var values = [];
items.each(function() {
values.push(this.value || $(this).text());
});
return name + '=' + values.join(',');
}

var result = [
buildList($('.productCodeID > .productitemcell'), 'skulist'),
buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

var string = result.join('&');

Here is the raw code before the javascript runs

这是javascript运行之前的原始代码

<span class="productPriceID">
<div class="productitemcell">.00</div>
<div class="productitemcell">.50</div>
<div class="productitemcell">.50</div>
</span>

回答by user113716

EDIT:Starting over with answer now that I have the code that is running.

编辑:现在我有正在运行的代码,从答案开始。

Looking at your updated code, this should work:

查看您更新的代码,这应该有效:

Example:http://jsbin.com/ekege3/

示例:http : //jsbin.com/ekege3/

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

result[ 2 ] = result[ 2 ].replace(/$/g, '');

var string = result.join('&');


Side note:You can shorten your buildListfunction a little like this:

旁注:您可以buildList像这样缩短您的功能:

function buildList(items, name) {
    return (name + '=') + items.map(function() {
        return (this.value || $(this).text());
    }).get().join(',');
}


Original answer:

原答案:

If you have a string, just use .replace().

如果你有一个字符串,只需使用.replace().

var str = "pricelist=.00,.50,.50";

str = str.replace(/$/g, '');

Or are you saying that you have a variable pricelistcontaining an Array? If so, do this:

var pricelist = [".00",".50",".50"];

for( var i = 0, len = pricelist.length; i < len; i++ ) {
    pricelist[ i ] = pricelist[ i ].replace('$', '');
}

EDIT:It sounds as though the buildListmethod returns an Array.

One way to check would be to do this:

alert( Object.prototype.toString.call( result[2] ) );

And see what it gives you.

Anyway, assuming it's an Array, here's the updated version of the second example.

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

// verify the data type
alert( Object.prototype.toString.call( result[ 2 ] ) );

// loop over result[ 2 ], replacing the $ with ''
for( var i = 0, len = result[ 2 ].length; i < len; i++ ) {
    result[ 2 ][ i ] = result[ 2 ][ i ].replace('$', '');
}

var string = result.join('&');
var str = "pricelist=.00,.50,.50";

str = str.replace(/$/g, '');

或者你是说你有一个pricelist包含数组的变量?如果是这样,请执行以下操作:

var pricelist = [".00",".50",".50"];

for( var i = 0, len = pricelist.length; i < len; i++ ) {
    pricelist[ i ] = pricelist[ i ].replace('$', '');
}

编辑:听起来好像该buildList方法返回一个数组。

检查的一种方法是这样做:

alert( Object.prototype.toString.call( result[2] ) );

看看它给了你什么。

无论如何,假设它是一个数组,这里是第二个示例的更新版本。

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

// verify the data type
alert( Object.prototype.toString.call( result[ 2 ] ) );

// loop over result[ 2 ], replacing the $ with ''
for( var i = 0, len = result[ 2 ].length; i < len; i++ ) {
    result[ 2 ][ i ] = result[ 2 ][ i ].replace('$', '');
}

var string = result.join('&');

回答by kaleazy

var price = $("div").text().replace("$", "");