从价格中删除美元符号

Mik*_*ler 15 javascript regex string

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

  buildList($('.productPriceID > .productitemcell'), 'pricelist')
Run Code Online (Sandbox Code Playgroud)

它正在回归

pricelist=$15.00,$19.50,$29.50
Run Code Online (Sandbox Code Playgroud)

我需要删除美元符号,但似乎无法弄明白.尝试使用.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('&');
Run Code Online (Sandbox Code Playgroud)

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

<span class="productPriceID">
<div class="productitemcell">$15.00</div>
<div class="productitemcell">$19.50</div>
<div class="productitemcell">$29.50</div>
</span>
Run Code Online (Sandbox Code Playgroud)

use*_*716 26

编辑:现在回答我有正在运行的代码.

查看更新的代码,这应该工作:

示例: 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('&');
Run Code Online (Sandbox Code Playgroud)

附注:你可以buildList像这样缩短你的功能:

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

原始答案:

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

var str = "pricelist=$15.00,$19.50,$29.50";

str = str.replace(/\$/g, '');
Run Code Online (Sandbox Code Playgroud)

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

var pricelist = ["$15.00","$19.50","$29.50"];

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

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

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

alert( Object.prototype.toString.call( result[2] ) );
Run Code Online (Sandbox Code Playgroud)

看看它给你的东西.

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

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('&');
Run Code Online (Sandbox Code Playgroud)


kal*_*azy 6

var price = $("div").text().replace("$", "");
Run Code Online (Sandbox Code Playgroud)