Removing dollar signs from prices

前端 未结 2 726
后悔当初
后悔当初 2020-12-06 10:11

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

  buildList($(\'.productPriceID > .productitemcell\'), \'p         


        
相关标签:
2条回答
  • 2020-12-06 10:48
    var price = $("div").text().replace("$", "");
    
    0 讨论(0)
  • 2020-12-06 11:01

    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/

    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 buildList function a little like this:

    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().

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

    EDIT: 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] ) );
    

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

    0 讨论(0)
提交回复
热议问题