Удаление знаков доллара от цен

Я строю ряд сумм, но мне нужно удалить знаки доллара. У меня есть этот код 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>

Ответы

Ответ 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('$', '');
}

EDIT: Кажется, что метод 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('&');

Ответ 2

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