weixin_33704591 2015-10-21 14:58 采纳率: 0%
浏览 20

模式对话框中的Ajax Spinner

I am trying to display the ajax spinner in modal dialog. I have an ajax spinner already in my page in one of the ajax action for which initially I have coded it using the following code.

$(document)
      .ajaxStart(function () {
              $loading.show();
      })
      .ajaxStop(function () {
              $loading.hide();
});

But this is too generic, now I want to show the spinner in one of the modal popup and I tried using the following code

$('#modal_loading_div').bind("ajaxStart", function(){
  alert('In ajax Start of modal dialog');
  $modalLoading.show();
});
$('#modal_loading_div').bind("ajaxStop", function(){
  alert('In ajax Stop of modal dialog');
  $modalLoading.hide();
});   

The above code is not called single time and control is not flowing. As an immediate tweak I tried my way doing this

$(document)
      .ajaxStart(function () {
          if(dialog){
              $('#delete_loading_div').show();  
          }else{
              $loading.show();
          }
      })
      .ajaxStop(function () {
          if(dialog){
              $('#delete_loading_div').hide();  
          }else{
              $loading.hide();
          }
});

but want to know the right way to do this. I am using the jquery-2.1.4

  • 写回答

1条回答 默认 最新

  • weixin_33701251 2015-10-21 15:40
    关注

    There's not really a 'correct' way of doing this.

    As of jQuery 1.8, the .ajaxStart() method should only be attached to document. (https://api.jquery.com/ajaxStart/)

    If you're binding on ajaxStart, then the code you've shared is really the only way of differentiating between whether the 'modal' or 'general' spinner should show. I imagine you have something like:

    $('.open-modal-dlg').click(function(){
        dialog = true;
        $.ajax(...);
        ...
    });
    $('.close-modal-dlg').click(function(){
        dialog = false;
        ...
    });
    

    That's fine, as it is.

    The only alternative I can see is to not use $.ajaxStart, and instead handle displaying the spinner on each of your AJAX calls.

    $('.loading').show();
    $.ajax(...).always(function(){
        $('.loading').hide();
    });
    
    // -- for modals...
    
    $('.loading-modal').show();
    $.ajax(...).always(function(){
        $('.loading-modal').hide();
    });
    

    .ajaxStart is a global AJAX jQuery event, so there's no way of limiting it, apart from keeping track of a separate variable yourself.

    评论

报告相同问题?

悬赏问题

  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?
  • ¥50 复现论文;matlab代码编写
  • ¥30 echarts 3d地图怎么实现一进来页面散点数据和卡片一起轮播
  • ¥15 数字图像的降噪滤波增强
  • ¥15 心碎了,为啥我的神经网络训练的时候第二个批次反向传播会报错呀,第一个批次都没有问题