【问题标题】:Bootbox dialog.modal('hide') doesn't hide modal?Bootbox dialog.modal('hide') 不隐藏模式?
【发布时间】:2019-11-18 17:49:04
【问题描述】:

我对 bootbox.js 模式有疑问。 正在执行并等待响应就绪状态更改为 3,模型将显示 但是在请求完成并且就绪状态更改为 4 并且 readystate 4 在 console.log 中打印但模型没有隐藏之后。

    var dialog = bootbox.dialog({
    message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we do something...</p>',
    className: 'bounceInUp animated',
    closeButton: false,
    show: false
                                  });

// ready stat with jquery
var _orgAjax = jQuery.ajaxSettings.xhr;

jQuery.ajaxSettings.xhr = function () {
    var xhr = _orgAjax();
    xhr.onreadystatechange = function() {

      console.log(xhr.readyState);

      var state = xhr.readyState;


      if (state == 3 ) {

      dialog.modal('show');
      console.log('readystate 3 ');

      }else if (state == 4) {

        dialog.modal('hide');
        console.log('readystate 4 ');

      };




    }
   return xhr;
};

【问题讨论】:

  • 如果你使用 jquery 为什么你不使用 $.ajax ??
  • 我在这段代码之后使用了它

标签: javascript php jquery ajax bootbox


【解决方案1】:

您可以通过选项beforeSend 在 ajax 请求中使用它,并在 ajax 完成后始终关闭它。

$.ajax({
  beforeSend: function() {
    dialog.modal('show'); <------------------ HERE show before
  },
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
    alert( "Data Saved: " + msg );
})
.always(function( msg ) {
    dialog.modal('show'); <----------------- HERE hide always on finish
 });

您也可以像this link 一样使用$.ajaxSetup,但从文档中不建议使用它

【讨论】:

  • 感谢您的帮助,但在尝试您的代码后,我收到此错误 -> SyntaxError: expected expression, got '.' --> 这个。 .always 之前的功能
  • @TarekRamadan 啊,多余的;完成后。已更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
相关资源
最近更新 更多