Здравствуйте, я создал функцию javascript, которая пересчитывает общую сумму заказа при изменении метода доставки. Код прекрасно работает в Firefox, Chrome и т. Д. 9, но возвращает NAN в т. Е. 8 и 7, что, как я понимаю, означает не число.
Я пытался использовать parseInt, pareseFloat и Number, но не могу понять, почему он не работает в 6, 7 или 8
мой код выглядит следующим образом
<script type="text/javascript">
function ChangePrice(a)
{
// grabs the delivery price of the selected option i.e 10.99
var price = parseFloat(a);
// grab delivery price which is currently on screen i.e 4.99
old_price = document.getElementById('delivery-sidebar').innerHTML;
// removing the span tags around the function which are not needed
old_price = old_price.replace('<SPAN class=price>','');
old_price = old_price.replace('</SPAN>','');
//grab subtotal price (which does not include delivery) which is currently on screen i.e 34.99
var subtotal = document.getElementById('overall').innerHTML;
// removing the span tags around the function which are not needed
subtotal = subtotal.replace('<span class="price">','');
subtotal = subtotal.replace('</span>','');
subtotal = subtotal.replace('£','');
// converting subtotal to float
subtotal=parseFloat(subtotal);
// if the price of the delivery does not match the price currently on screen
if (price != old_price)
{
//add new price against subtotal
var overall_total = (parseFloat(subtotal))+(parseFloat(price));
// round result to two decimal places i.e 10.99
overall_total=roundNumber(overall_total,2);
// update values on screen
document.getElementById('delivery-sidebar').innerHTML = '<span class="price">£'+price.toFixed(2)+'</span>';
document.getElementById('grand-price').innerHTML = '<span class="price">£'+overall_total.toFixed(2)+'</span>';
}
}
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
</script>
и разметка
<ul>
<li>
<input name="shipping_method" value="tablerate_bestway" id="s_method_tablerate_bestway" checked="checked" class="radio validation-passed" onclick="ChangePrice('0')" type="radio">
<label for="s_method_tablerate_bestway">UK Delivery (3 - 7 Working Days) </label>
<span class="price">Free delivery</span>
</li>
</ul>
<dd>
<ul>
<li>
<input name="shipping_method" value="flatrate_flatrate" id="s_method_flatrate_flatrate" class="radio validation-passed" onclick="ChangePrice('10')" type="radio">
<label for="s_method_flatrate_flatrate">Next Day - Orders Before 2pm </label>
<span class="price"><span class="price">£10.00</span></span>
</li>
</ul>
</dd>