ajax - 将模态链接到按钮
问题描述
https://www.fermento24.com - https://www.fermento24.com/collections/all
使用 Shopify 完成的网站。由于在主页上它使用网站完成的标准材料,它会自动链接到它的模态 ajax。
同时,收集页面(第二个链接)是由一个显示过滤器和东西的应用程序生成的。我想做的是将购买按钮链接到此模式弹出窗口,而不是将我带到购物车页面。不知道这个怎么过,是不是应该在模态里做修改呢,还是收藏页模板里做修改呢?
解决方案
您可以尝试下面列出的代码,并且可能需要根据您的主题进行一些更新。
需要注意:商品加入购物车后,您需要检查并创建一个更新迷你购物车的方法。
window.onload = function(){
// disable the default behavior
$('.boost-pfs-addtocart-btn').each(function(index, button){
var $this = $(button);
$this.closest('form').on('submit', function(e){
e.preventDefault();
});
});
// create new logic once the button clicked
$(document).on('click', '.boost-pfs-addtocart-btn', function(){
var form = $(this).closest('form');
$.post('/cart/add.js', form.serialize(), function(data, status){
if( status == 'success' ){
// show popup once added to cart
var model = $(".ajax-success-modal");
model.fadeIn(500);
var item = form.parents('.boost-pfs-filter-product-item-inner')[0]
var imgSrc = $(item).find('.boost-pfs-filter-product-item-main-image').attr('src');
var pTitle = $(item).find('.boost-pfs-filter-product-item-title').text();
var CPrice = $(item).find('.boost-pfs-filter-product-item-regular-price').text();
model.find(".ajax-product-image").attr("src", imgSrc);
model.find(".added-to-wishlist").hide();
model.find(".added-to-cart").show();
model.find(".ajax-product-title").text(pTitle);
model.find(".ajax_price").text(CPrice);
model.find(".ajax_qty").text(1);
// updating the cart counter in header
$.get("/cart.js", function(data){
$('#cartCount,.ajax_qtyA').text('').text(data.item_count);
$('.ajax_cartTotal').text(Shopify.formatMoney(data.total_price, window.money_format));
}, 'json');
}
}, 'json');
});
}
请在任何 JS 文件中或在结束之前将代码</body>
放入<script></script>
标签中,如果将其放在正文标签之前。
注意:还要确保在代码运行之前加载 jQuery,否则代码会由于依赖 jQuery 库而失败。
推荐阅读
- javascript - Cytoscape.js 网格布局不使用网格位置
- php - 在 Guzzle 中解决多个异步承诺时,我只能从第一个承诺中获得结果
- ios - 卡片样式 CollectionViewCell
- html - 如何在包含滚动条的父 div 中垂直居中子 div?
- java - Java Spring:请求的资源上不存在“Access-Control-Allow-Origin”标头
- javascript - create-react-app:无法安装站点:未检测到匹配的服务人员
- perl - 获取一年中的一周(一周的第一天是星期日)无法返回第 53 周
- swift - 如何从可解码的枚举中获取原始值?迅速
- ios - -canOpenURL:URL 失败:“fbauth2:/” - 错误:“此应用不允许查询方案 fbauth2”(OSStatus 错误 -10814。)
- optimization - 用于切割股票算法的 Scilab