遵循Django教程,其中一部分是创建一个Ajax请求来刷新/更新购物车。我被困在这个特定的界限上:
var currentPath = window.location.href
if (currentPath.indexOf("carts") != -1) {
updateCart()
购物车指向什么,这是指向另一个HTML网页的链接还是引用了一个类?
下面的完整JS:
$(document).ready(function(){
var productForm = $(".form-product-ajax") // #form-product-ajax
productForm.submit(function(event){
event.preventDefault();
// console.log("Form is not sending")
var thisForm = $(this)
//var actionEndpoint = thisForm.attr("action");
var actionEndpoint = thisForm.attr("data-endpoint");
var httpMethod = thisForm.attr("method");
var formData = thisForm.serialize();
$.ajax({
url: actionEndpoint,
method: httpMethod,
data: formData,
success: function(data){
var submitSpan = thisForm.find(".submit-span")
if (data.added){
submitSpan.html("In cart <button type='submit' class='btn btn-link'>Remove?</button>")
} else {
submitSpan.html("<button type='submit' class='btn btn-success'>Add to cart</button>")
}
var currentPath = window.location.href
if (currentPath.indexOf("carts") != -1) {
updateCart()
}
},
error: function(errorData){
console.log("error")
console.log(errorData)
}
})
})
function updateCart(){
console.log("in current cart")
var cartTable = $(".cart-table")
var cartBody = cartTable.find(".cart-body")
cartBody.html("<h1></h1>")
}