【问题标题】:Problem displaying and hiding a Spinner on AJAX calls在 AJAX 调用中显示和隐藏 Spinner 时出现问题
【发布时间】:2018-10-19 19:34:42
【问题描述】:

我正在创建一个可以正常工作的表单,并使用 AJAX 将结果发布到后端。为了增强用户体验,我添加了一个微调器,当用户触发单击表单中的按钮的事件时应显示该微调器,并在 AJAX 请求成功后立即删除微调器,我正在尝试添加或删除使用不起作用的 CSS 的动态类

布局

<div class="container PAY">
     <div class="card-deck payment">
      <div class="card">
        <img class="card-img-top payment" src="{{asset('assets/images-new/mpesa.svg')}}" alt="Card image cap">
        <div class="card-body">
          <h5 class="card-title payment"><a href="#" id="mpesa" class="mpesa">Make Payment</a></h5>
        </div>
      </div>
<div class="modal" id="modal"><!-- Place at bottom of page --></div>
</div>

CSS

.modal {
    display:    none;
    margin-top: 10%;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     400px;
    width:      100%;
    background: rgba( 255, 255, 255, .8 ) 
                url('../images/ajax-loader-2.gif') 
                50% 50% 
                no-repeat;
}

/*Make sure the modal class is hidden by default and we control the style using Js*/
.PAY.loading .modal {
    display:none;   
}

AJAX 调用以添加或删除按钮

<script type="text/javascript">
  //Trigger button
$('.mpesa').on('click', function () {
    //Adds Class to the page when it loads
    ajaxStart: function() { #PAY.addClass("loading");    },
    $.ajax({
        //Contains controller of payment
        type: 'POST',
        url: 'paymentFinal',
        data: JSON.stringify(type),
        contentType: 'application/json',
        dataType: "json",
        success: function success(response) {
            console.log(response);
             //Removes class when the page loads
            ajaxStart: function() { #PAY.removeClass("loading");}
        },
        error: function error(data) {
            console.log(data);
        }
    });
});

</script>

【问题讨论】:

  • #PAY 是无效的 JavaScript。你的意思是$('.PAY')? (另外,ajaxStart: ... 的形状和/或位置非常奇怪......)
  • @Amadan Yah 确定我是按类获取元素,已纠正但仍然无效。

标签: ajax css spinner


【解决方案1】:

您要么使用全局 ajaxStart/ajaxStop 处理程序,要么根据每个请求打开/关闭。

这是每个请求的一个(可能是因为我没有测试它是否有其他错误):

$('.mpesa').on('click', function () {
    //Adds Class to the page when it loads
    $('.PAY').addClass("loading");

    $.ajax({
        //Contains controller of payment
        type: 'POST',
        url: 'paymentFinal',
        data: JSON.stringify(type),
        contentType: 'application/json',
        dataType: "json",
        success: function success(response) {
            console.log(response);

             //Removes class when the page loads
            $('.PAY').removeClass("loading");
        },
        error: function error(data) {
            console.log(data);
        }
    });
});

当几个请求碰巧重叠时,这有可能做错事,所以这样更好:

// Do this once only, then forget about it
$(document)
  .ajaxStart(function() { $('.PAY').addClass("loading"); })
  .ajaxStop(function() { $('.PAY').removeClass("loading"); })

然后在代码中再也不提及.PAY、loading 或ajaxStart。

【讨论】:

  • ,,谢谢它工作正常,,,我没有低估第二个代码块,,你的意思是我将它添加到我的 Jquery 代码的顶部
  • 在代码代码的顶部。脚本的开始。不确定“我的 jQuery 代码”是什么意思。
  • ,, 我明白了,, 非常感谢,, 微调器运行良好.. 当用户在 AJAX 请求完成之前点击页面的任何部分时是否可以停止它
  • 当然。处理点击事件并移除加载类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
相关资源
最近更新 更多