我使用的是带有自定义ajax代码的add to cart,它的工作情况很好,问题是我需要使用timber.RightDrawer.open()来打开购物车的抽屉;现在,我在形式上使用“js-抽屉-打开-右”类,但当单击add按钮时,它的打开抽屉同时使用。我需要打开抽屉上的成功添加到手推车。
我的Ajax代码是:
function addItem(button) {
var postData = $(button).closest('.add-to-cart').serialize();
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: postData,
success: addToCartOk,
error: addToCartFail
});
}
function addToCartOk(product) {
//Want to open drawer here on success
timber.RightDrawer.open();
}
function addToCartFail(obj, status) {
}我的表格是:
在这里您可以检查Add to Cart https://soft-theme.myshopify.com/collections/all
发布于 2017-03-26 20:55:18
与timber.RightDrawer.open()相比,我找到了不同且非常简单的解决方案;
我在成功函数中用jQuery点击了“js-抽屉-打开-右”,并将该类从我之前放置的表单中删除。
现在的成功职能是:
function addToCartOk(product) {
//drawer open here by click on that class on success
jQuery('.js-drawer-open-right').click();
}它工作得很好。
发布于 2018-05-06 10:54:27
@barjinder解决方案,但更清洁:
$(document).on("click", ".add-to-cart-button", function(event) {
event.preventDefault();
var form = $(this).closest('form');
jQuery.ajax({
type: 'POST',
url: '/cart/add.js',
data: form.serialize(),
dataType: 'json',
success: function(cart) {
jQuery('.js-drawer-open-right').click();
},
error: function(XMLHttpRequest, textStatus) {
alert("An error occurred: can't reach server");
},
});
});您可以使用下面的表单
<form action="/cart/add" method="POST">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="id" value="{{ product.variants.first.id }}">
<button type="submit" class="add-to-cart-button">
Add to cart
</button>
</form>https://stackoverflow.com/questions/43031425
复制相似问题