【问题标题】:Trigger jQuery After an Ajax Call - Plugin Ajax Layered Navigation - WooCommerce在 Ajax 调用后触发 jQuery - 插件 Ajax 分层导航 - WooCommerce
【发布时间】:2020-02-07 10:48:26
【问题描述】:

经过搜索,我发现了类似的问题,但没有一个与我的问题或适合我的解决方案直接相关。我的 jQuery 知识不是太强,所以它可能是好的和简单的。

来了……

• 我有一个 WooCommerce 网站并安装了 Ajax 分层导航插件来过滤类别。

• 要为这些添加特定样式,我有以下 jQuery 函数来为每个 li 添加一个类

$('.checkboxes li').each(function(){
$(this).addClass("filter_" + $(this).text());
});

• 在选择过滤器之前效果很好。

• 重新加载页面后,上述类将被删除。

• 我已经考虑过围绕 ajaxSuccess 和 ajaxComplete 组合许多不同的选项,但是无法重新触发该功能。


我需要知道的是如何重新触发上述事件,以便每次通过 ajax 重新加载/过滤页面时,都会将相应的类添加到该 li。

This is the code before Ajax takes place, all li's have the right class

This image shows the Li's after the filter has been clicked

$( document ).ajaxComplete(function( event,request, settings ) { 
    $( "#products" ).append( "<li>Request Complete.</li>" ); 
});

$( document ).ajaxSuccess(function() { 
    $('.checkboxes li').each(function(){
        $(this).addClass("filter_" + $(this).text());
    });
}); 

$(document).ajaxSuccess(function(){ 
    alert("AJAX request successfully completed");
});

非常感谢您的任何帮助!

【问题讨论】:

  • 确保在$(document).ready函数中编写此代码
  • 感谢 Krunal,问题中的函数已经在 $(document).ready 中 - 我知道它会在页面加载时加载函数并且运行良好。我遇到的问题是当通过 ajax 重新加载页面时,此函数不会重新触发。不过感谢您的快速回复!
  • 可以分享网址吗?
  • 很遗憾,由于保密协议,无法在此处分享网址
  • 好吧,如果它在 ajax 之后更新,那么 ajaxComplete 将对此有所帮助,请在其中写下并尝试提醒并检查是否发出警报 - api.jquery.com/ajaxComplete 并在 @ 内检查987654328@ 在ajaxComplete 内,因为更新下拉列表花费了很少的时间

标签: javascript jquery ajax wordpress woocommerce


【解决方案1】:

您是否尝试在 ajax 完成后设置超时?

$( document ).ajaxComplete(function() { 

    setTimeout(function(){ 

      alert("Ajax done"); 

      $('.checkboxes li').each(function(){
        $(this).addClass("filter_" + $(this).text());
      });

    }, 500); 

});

【讨论】:

    猜你喜欢
    • 2016-01-28
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    相关资源
    最近更新 更多