简体   繁体   English

如何从jquery中准备好的文档中调用ajaxComplete内部的函数?

[英]How to call function inside ajaxComplete from document ready in jquery?

I want to execute function inside .ajaxComplete() from document ready. 我想从文档准备好在.ajaxComplete()执行函数。

$(function() {

      $(document).on('click', '.woof_radio_label', function(e) {
        if($(this).siblings('.woof_radio_term').is(':checked')) {
          $('.page-secondary-content').removeClass('open');
          $('.page-primary-content').removeClass('close');

          $(this).siblings('.woof_radio_term_reset').click();

          refineUrl();

        } else {
          $('.page-primary-content').addClass('close');
          $('.page-secondary-content, .bottom-content').addClass('open');
        }
      });

});

$(document).ajaxComplete(function() {
  function refineUrl() {
   console.log('it works');
  }
}); 

I am prompted with an error below: 出现以下错误提示我:

Uncaught ReferenceError: refineUrl is not defined at HTMLLabelElement. 未捕获的ReferenceError:HTMLLabelElement上未定义finefineUrl。 (scripts-custom.js?ver=4.9.7:15) at HTMLDocument.dispatch (jquery.js?ver=1.12.4:3) at HTMLDocument.r.handle (jquery.js?ver=1.12.4:3) (HTMLscript.dispatch中的(scripts-custom.js?ver = 4.9.7:15)(HTMLDocument.r.handle(jquery.js?ver = 1.12.4:3))(jquery.js?ver = 1.12.4:3) )

When I click the class woof_radio_label it will trigger an ajax event(Product Filter), I want to call a function on ajaxComplete(). 当我单击woof_radio_label ,它将触发ajax事件(产品过滤器),我想在ajaxComplete()上调用一个函数。

Do you know how to do this? 你知道怎么做吗?

You should declare function refineUrl() globally and then it can be called from anywhere. 您应该全局声明函数refineUrl() ,然后可以在任何地方调用它。 From ajaxComplete and from document.ready as well. 也来自ajaxCompletedocument.ready

Like: 喜欢:

$(function () {
    $(document).on('click', '.woof_radio_label', function (e) {
        if ($(this).siblings('.woof_radio_term').is(':checked')) {
            $('.page-secondary-content').removeClass('open');
            $('.page-primary-content').removeClass('close');

            $(this).siblings('.woof_radio_term_reset').click();

            refineUrl();

        } else {
            $('.page-primary-content').addClass('close');
            $('.page-secondary-content, .bottom-content').addClass('open');
        }
    });

});

$(document).ajaxComplete(function () {
    //DO another stuff
});
function refineUrl() {
    console.log('it works');
}

Declare your refineUrl() at the top of your script to prevent from such type of errors like, 在脚本顶部声明您的refineUrl() ,以防止发生此类错误,

function refineUrl() {
    console.log('it works');
}
$(function(){
     .....
});
$(document).ajaxComplete(function() {
   refineUrl(); 
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery-如何在ajaxComplete上创建新函数来模拟document.ready? - jQuery - How to create a new function to simulate document.ready on ajaxComplete? 从函数内部调用$(document).ready()是否安全? - Is it safe to call $(document).ready() from inside a function? 如何从 document.ready 调用 jQuery 函数 - How to call jQuery function from document.ready 在$('document')中调用一个函数。准备好了 - Call a function inside $('document').ready 从文档就绪事件中的导入模块调用 function 是否安全? - Is it safe to call a function from imported module inside document ready event? 尝试从 jquery $(document).ready 调用外部 js 中定义的 function - Try to call function defined in external js from jquery $(document).ready jQuery将document.ready和ajaxComplete方法组合在一起以实现相同的代码 - jQuery combining document.ready and ajaxComplete methods together for same code 如何使用setTimeout()调用jQuery(document).ready之外的函数? - How to call function outside of jQuery(document).ready with setTimeout()? 如何在 document.ready 上调用多个 js 函数而不放置它们 jQuery(document).ready(function(){}); - How to call multiple js functions on document.ready without putting them jQuery(document).ready(function(){}); 如何在jQuery中的文档就绪事件上自动调用函数 - How to call a function automatically on document ready event in jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM