我有以下使用Ajax的代码,我想用javascript fetch()进行转换,请有人帮我做一下吗? (请提供一些解释,而不仅仅是代码)
谢谢。
function save_to_db(cart_id, new_quantity, newPrice) {
var inputQuantityElement = $("#input-quantity-"+cart_id);
var priceElement = $("#cart-price-"+cart_id);
$.ajax({
url : "update_cart_quantity.php",
data : "cart_id="+cart_id+"&new_quantity="+new_quantity,
type : 'post',
success : function(response) {
$(inputQuantityElement).val(new_quantity);
$(priceElement).text("$"+newPrice.toFixed(2));
var totalQuantity = 0;
$("input[id*='input-quantity-']").each(function() {
var cart_quantity = $(this).val();
totalQuantity = parseInt(totalQuantity) + parseInt(cart_quantity);
});
$("#total-quantity").text(totalQuantity);
var totalItemPrice = 0;
$("div[id*='cart-price-']").each(function() {
var cart_price = $(this).text().replace("$","");
totalItemPrice = (parseInt(totalItemPrice) + parseInt(cart_price)).toFixed(2);
});
$("#total-price").text(totalItemPrice);
$("#total-price-foot").text(totalItemPrice);
}
});
}