【问题标题】:ajaxstart is triggered in a toggle() event but Cant triger ajaxstop on Completeajaxstart 在 toggle() 事件中触发,但无法在 Complete 上触发 ajaxstop
【发布时间】:2014-01-30 10:30:59
【问题描述】:

我有一个函数可以处理通过单击某个切换按钮触发的 div 的切换。

$('.r_eply_togler').click(function() {
   var id = $(this).attr('id');
   var nw_id = '#q_reply_form_' + id;
   $(nw_id).toggle();

  });

我有另一个带有 ajaxstart 和 ajaxstop 的函数来显示 ajax 调用期间的加载。

function ajaxcompleted(){
  $(document).ajaxStart(function(){
  $('#spinner').show();
}); 

   $(document).ajaxStop(function(){
    $('#spinner').hide();
    }); 
   }

但是我不希望在单击切换按钮时触发 ajaxstart 和 ajaxstop。目前,ajaxcompleted 函数在单击切换按钮时被触发,但不是正确的,因为它不会停止,我猜是因为当时没有发生 ajax 调用。

有没有一种方法可以避免在单击切换按钮时触发 ajaxcompleted 功能,或者有没有一种方法可以让它正常运行,显示加载更多然后隐藏。

解决方案

我已经找到了解决方案。我在切换功能中添加了 return false 并且它起作用了。问题是 ajaxCompelted() 函数无法识别切换事件执行的结束。

function togle(){
      $('.r_eply_togler').click(function() {
           var id = $(this).attr('id');
           var nw_id = '#q_reply_form_' + id;
           $(nw_id).toggle();
            return false;
          });   
    }

谢谢大家的回复。

【问题讨论】:

  • 我想知道您发布的代码中.toggle().ajaxCompelted() 连接在哪里。你能发布完整的代码吗
  • 在这种情况下,其余代码将无关紧要。这就是问题所在,ajaxcompleted() 默认连接到所有 jquery 事件。但我不希望它影响切换事件。
  • Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time. ref:api.jquery.com/ajaxstart
  • 感谢您的提示。检查已编辑问题中的解决方案。
  • ok,很酷,将此解决方案添加为答案并将其选中为答案,以便关闭此问题。

标签: javascript jquery html ajax


【解决方案1】:

您可以将其用于您的解决方案 http://jqueryui.com/toggle/

【讨论】:

  • 发布您的答案作为解决方案,然后@jmsiox
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多