2016-04-11 11 views
0

Ich versuche, einen Preis durch JQuery zu ändern. Wenn ich den Code der Konsole hinzufüge, scheint alles in Ordnung zu sein, aber der HTML-Code ändert sich nicht. Auch wenn ich die console.log(newPrc) anrufe sagt es "newPrc" nicht definiert. Mein Code ist:jQuery ändert HTML nicht wie beabsichtigt

(function($) { 
 
    Number.prototype.formatMoney = function(c, d, t) { 
 
    var n = this, 
 
     c = isNaN(c = Math.abs(c)) ? 2 : c, 
 
     d = d == undefined ? "." : d, 
 
     t = t == undefined ? "," : t, 
 
     s = n < 0 ? "-" : "", 
 
     i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
 
     j = (j = i.length) > 3 ? j % 3 : 0; 
 
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); 
 
    }; 
 

 
    $(document).ready(function() { 
 

 
    oldPrc = jQuery('body.checkout-delivery .basket__totals #grand-total-price .price').html(); 
 
    newPrc = oldPrc.replace('<span class="currency">£</span>', ''); 
 
    newPrc = newPrc.split(','); 
 
    newPrc = newPrc[0] + newPrc[1]; 
 
    newPrc = parseInt(newPrc) + 39; 
 
    newPrc = '<span class="currency mtI">£</span>' + newPrc; 
 
    jQuery('body.checkout-delivery .basket__totals #grand-total-price .price:has(.currency:not(.mtI))').html(newPrc); 
 
    }); 
 

 

 
})(jQuery);

+0

ist oldPrc und newPrc anderswo definiert? Wenn nicht, müssen Sie sie mit 'var' definieren, was den Konsolenfehler erklären würde. –

+0

Wo setzt du console.log (newPrc)? – m4tt1mus

+0

Ich setze es auf die Konsole, um den Wert von newPrc – John

Antwort

1

Sobald ich habe jQuery und den entsprechenden Demo-Markup das Snippet hinzugefügt es scheint hinzuzufügen 39 (auch nicht wirklich als 39 zuzüglich 4,99 ist nicht 43) an den ursprünglichen Preis, wie Sie beabsichtigten und im HTML zeigt das Ergebnis:

(function($) { 
 
    Number.prototype.formatMoney = function(c, d, t) { 
 
    var n = this, 
 
     c = isNaN(c = Math.abs(c)) ? 2 : c, 
 
     d = d == undefined ? "." : d, 
 
     t = t == undefined ? "," : t, 
 
     s = n < 0 ? "-" : "", 
 
     i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
 
     j = (j = i.length) > 3 ? j % 3 : 0; 
 
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); 
 
    }; 
 

 
    $(document).ready(function() { 
 

 
    oldPrc = jQuery('body.checkout-delivery .basket__totals #grand-total-price .price').html(); 
 
    newPrc = oldPrc.replace('<span class="currency">£</span>', ''); 
 
    newPrc = newPrc.split(','); 
 
    newPrc = newPrc[0] + newPrc[1]; 
 
    newPrc = parseInt(newPrc) + 39; 
 
    newPrc = '<span class="currency mtI">£</span>' + newPrc; 
 
    jQuery('body.checkout-delivery .basket__totals #grand-total-price .price:has(.currency:not(.mtI))').html(newPrc); 
 
    }); 
 

 

 
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<body class="checkout-delivery"> 
 
    <div class="basket__totals"> 
 
    <div id="grand-total-price"> 
 
     <div class="price"><span class="currency">£</span>4.99</div> 
 
    </div> 
 
    </div> 
 
</body>

Sieht Ihr Markup wie das oben stehende Markup aus?

Verwandte Themen