【问题标题】:Removing dollar signs from prices从价格中去除美元符号
【发布时间】:2011-05-11 16:22:51
【问题描述】:

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

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

它回来了

pricelist=$15.00,$19.50,$29.50

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

抱歉新手问题!提前感谢您的帮助!

这是完整的代码:

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 运行前的原始代码

<span class="productPriceID">
<div class="productitemcell">$15.00</div>
<div class="productitemcell">$19.50</div>
<div class="productitemcell">$29.50</div>
</span>

【问题讨论】:

  • 这与JQuery无关,在这种情况下只是javascript。
  • 是的。它被包裹在上面的一些jquery中。我已经包含了更多代码,希望它更有意义。

标签: javascript regex string


【解决方案1】:

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

查看您更新的代码,这应该可以:

示例: 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('&');

旁注:您可以像这样缩短 buildList 函数:

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, '');

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

var pricelist = ["$15.00","$19.50","$29.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('&');

【讨论】:

  • 我已经更新了上面的代码。基本上,价目表是根据价格列表构建的。我需要在此过程中的某个时候删除美元符号。感谢您的帮助!
  • @mmsa - 如果可以,请修改标记,以免一开始就收到 $。否则,您似乎在说buildList 返回一个字符串,所以我的第一个示例应该可以工作。如果您想在执行join() 之前执行此操作,则只需使用我的第一个示例,但将str 替换为result[ 2 ],如result[ 2 ] = result[ 2 ].replace(/\$/g, '')
  • 首先,感谢您的帮助!由于某种原因它不起作用。在上面的示例中,您的变量 str 是一个固定值。每个用户的这些美元数字都会改变。它是从购物卡中取出的。我把代码放在它上面,但由于某种原因它在 .replace 上产生了一个错误。
  • @patrick dw - 另一种想法,有没有一种简单的方法可以从原始数字中删除它?我已经粘贴了上面的原始代码。这是在 javascript 运行之前从 CMS 呈现的内容。我希望我能控制美元符号,但我没有。再次感谢!
  • @mmsa - 问题是我不知道buildList 返回的数据类型。现在我猜它是一个数组,这意味着您将使用第二个示例。在我的答案中更新它会更容易。给我一分钟。关于更改标记,您不妨照原样做,因为听起来您无法从服务器更改它。
【解决方案2】:
var price = $("div").text().replace("$", "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多