javascript - ajax 运行时加载显示模式

标签 javascript php jquery ajax zurb-foundation

您好,我正在从我的 php 页面调用 ajax,当 ajax 运行时,我想在基础显示模式上加载图像。

这在我的代码中。

<div id="loadinimg" class="reveal-modal" data-reveal>
        Loading!!!!!!!!!!!!!!!
</div>

   $('.print-button').click(function() {

        $.when(
        $.ajax({
                url: 'newphp1.php',
                type: "POST",
                beforeSend: function() {
                            $('#loadinimg').foundation('reveal', 'open');
                },
                data: {
                        selected_date: '<?php echo $GLOBALS["selected_date"] ?>'
                },
                error: function(data){
                },
                success: function(response){
                         alert(response);    
                         console.log(response);
                },
                complete: function() {
                    $('#loadinimg').foundation('reveal', 'close');
                }
         })).done(
               $.ajax({
                        url: 'newph2p.php',
                        type: "POST",
                        data: {
                                selected_date: '<?php echo $GLOBALS["selected_date"] ?>'
                            },
                        error: function(data){
                            },
                        success: function(response){
                            console.log(response);
                            console.log(response);
                            location.reload();
                        }
                    }));
         });

但只有在 ajax 执行结束时才会弹出显示模式。可能是什么问题?

最佳答案

您可以使用 ajaxbeforeSendcomplete 选项,如下所示:

$.ajax({
    url: 'newphp.php',
    type: "POST",
    async: false,
    beforeSend: function() {
                $('#loadinimg').foundation('reveal', 'open');
    },
    error: function(data){
    },
    success: function(response){
             alert(response);    
             console.log(response);
    },
    complete: function() {
             //close the loading image
    }
}

更新

$.when(
$.ajax({
        url: 'newphp.php',
        type: "POST",
        beforeSend: function() {
                    $('#loadinimg').foundation('reveal', 'open');
        },
        error: function(data){
        },
        success: function(response){
                 alert(response);    
                 console.log(response);
        },
        complete: function() {
                 //close the loading image
        }
 })).done(function(){
       //make another ajax call here
 });

更新2

这可能是现在唯一的出路了!只需在 js 文件中设置一个全局变量,如下所示:

var ajax1Complete=false;

$.when(
$.ajax({
        url: 'newphp.php',
        type: "POST",
        beforeSend: function() {
                    $('#loadinimg').foundation('reveal', 'open');
        },
        error: function(data){
        },
        success: function(response){
                 alert(response);    
                 console.log(response);
                 ajax1Complete=true;//set it on success
        },
        complete: function() {
                 //close the loading image
        }
 })).done(function(){
       if(ajax1Complete)
       {
          //make another ajax call here and onsuccess set ajax1Complete to false
       }
 });

关于javascript - ajax 运行时加载显示模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31019019/

相关文章:

javascript - jQuery 不断添加显示 : block to my table row

javascript - 使用 Javascript 填充数据以折叠

javascript - IE+jQuery+Ajax+XHTML : HTML getting clipped after . html() 或 .innerHTML

javascript - 如何通过他的内容得到一个HTML元素?

php - 无法从 EC2 WordPress 站点连接到 Amazon RDS

php PDO 和 mysql : how to insert a geo point type?

php - 可以在mysql数据末尾加一个值吗?

php - keyup if 语句的 indexOf 字符串行为很奇怪?

javascript - JS : how to filter table using variable

javascript - jQuery 处理按钮在表单内单击