admin管理员组

文章数量:1333658

I'm not sure where I'm going wrong but I'm trying to remove a € symbol from a String using jQuery as I'd like to be able to add the Values to a shopping cart.

Anyways, this is my fiddle: /

I'm calling the javascript replace function on a variable which I presume shouldn't matter?

If anyone can't see my fiddle this is the html:

 <div id='shopping-cart'>
                <span><span id='s-qty'>0</span> Items in Cart</span>
                <span>€<span id='s-total'>0.00</span></span>
</div>
<p class="book-price">€7.99</p><div class='add-to-cart'>Add to Cart</div>

and my jquery:

var cartitems = 0;

$('.add-to-cart').click(function(){
        var bookprice = $(this).parent().find('.book-price').text();
        bookprice.replace('€', '');
        cartitems = parseFloat(cartitems + 1);                   
        price = bookprice;
        $('#s-qty').html(cartitems);
        $('#s-total').html(price)
});

The above code is giving me the following output:

€€7.99 (the euro symbol is never being removed).

Probably a trivial question, so apologies but I'm at a loss so would appreciate any help.

Thank you.

I'm not sure where I'm going wrong but I'm trying to remove a € symbol from a String using jQuery as I'd like to be able to add the Values to a shopping cart.

Anyways, this is my fiddle: https://jsfiddle/javacadabra/uuu8px6r/1/

I'm calling the javascript replace function on a variable which I presume shouldn't matter?

If anyone can't see my fiddle this is the html:

 <div id='shopping-cart'>
                <span><span id='s-qty'>0</span> Items in Cart</span>
                <span>€<span id='s-total'>0.00</span></span>
</div>
<p class="book-price">€7.99</p><div class='add-to-cart'>Add to Cart</div>

and my jquery:

var cartitems = 0;

$('.add-to-cart').click(function(){
        var bookprice = $(this).parent().find('.book-price').text();
        bookprice.replace('€', '');
        cartitems = parseFloat(cartitems + 1);                   
        price = bookprice;
        $('#s-qty').html(cartitems);
        $('#s-total').html(price)
});

The above code is giving me the following output:

€€7.99 (the euro symbol is never being removed).

Probably a trivial question, so apologies but I'm at a loss so would appreciate any help.

Thank you.

Share Improve this question asked Feb 16, 2015 at 15:46 JavacadabraJavacadabra 5,76815 gold badges89 silver badges154 bronze badges 3
  • 1 Are you sure its and not &euro;? BTW replace is NOT an in-place operation, as strings are immutable. – meskobalazs Commented Feb 16, 2015 at 15:48
  • 1 You are going to confirm pricing with the server before you submit the cart, yes? – James Hill Commented Feb 16, 2015 at 15:49
  • @JamesHill yes definitely, but in this instance no as I'm not working with a DB it's just a demo version – Javacadabra Commented Feb 16, 2015 at 16:01
Add a ment  | 

6 Answers 6

Reset to default 2
bookprice = bookprice.replace('€', '');

The replace function is not an in-place operation in JavaScript, as strings are immutable. The following line replace the symbol and the &euro; HTML special character:

bookprice = bookprice.replace('€', '').replace('&euro;', '');
 bookprice = bookprice.replace('€', '');

works correctly, or

var bookprice = $(this).parent().find('.book-price').text().replace('€', '');

Just Use

bookprice = bookprice.replace('€', '');

The replace does not mutate the string it is executed on, the new value is returned instead. You need to store it in a variable.

bookprice = bookprice.replace('€', '');

In your case:

price = bookprice.replace(/€|&euro/g, '')

This solution uses regular expressions to replace both and html encoded &euro if present.

just do it

price = bookprice.replace(/\s*\u20ac\s*/ig,'')

本文标签: jqueryUsing Javascript Replace function to remove € Symbol from StringStack Overflow