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€
? 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
6 Answers
Reset to default 2bookprice = 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 €
HTML special character:
bookprice = bookprice.replace('€', '').replace('€', '');
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,'')
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742346092a4426571.html
评论列表(0条)