从价格中删除美元符号 [英] Removing dollar signs from prices
问题描述
我正在构建一系列金额,但需要删除美元符号。我有这个jQuery代码:
I'm building a string of amounts but need to remove the dollar signs. I have this jQuery code:
buildList($('.productPriceID > .productitemcell'), 'pricelist')
它正在返回
pricelist=$15.00,$19.50,$29.50
我需要删除美元符号但不能似乎弄清楚了。尝试使用.trim,但我认为只删除了空格。
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.
对不起新手问题!预先感谢您的帮助!
Sorry for the newbie question! Thanks in advance for any help!
以下是完整代码:
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('&');
以下是javascript运行前的原始代码
Here is the raw code before the javascript runs
<span class="productPriceID">
<div class="productitemcell">$15.00</div>
<div class="productitemcell">$19.50</div>
<div class="productitemcell">$29.50</div>
</span>
推荐答案
编辑:重新开始现在回答我有正在运行的代码。
Starting over with answer now that I have the code that is running.
查看更新的代码,这应该有效:
Looking at your updated code, this should work:
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('&');
旁注:您可以像这样缩短 buildList
函数:
Side note: You can shorten your buildList
function a little like this:
function buildList(items, name) {
return (name + '=') + items.map(function() {
return (this.value || $(this).text());
}).get().join(',');
}
原始答案:
如果你有一个字符串,只需使用 .replace()
。
var str = "pricelist=$15.00,$19.50,$29.50";
str = str.replace(/\$/g, '');
或者你是说你有一个变量价格表
包含一个数组?如果是这样,请执行以下操作:
Or are you saying that you have a variable pricelist
containing an Array? If so, do this:
var pricelist = ["$15.00","$19.50","$29.50"];
for( var i = 0, len = pricelist.length; i < len; i++ ) {
pricelist[ i ] = pricelist[ i ].replace('$', '');
}
编辑: 听起来似乎 buildList
方法返回一个数组。
It sounds as though the buildList
method returns an Array.
检查的一种方法是这样做:
One way to check would be to do this:
alert( Object.prototype.toString.call( result[2] ) );
看看它给你的是什么。
无论如何,假设它是一个数组,这是第二个例子的更新版本。
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('&');
这篇关于从价格中删除美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!