Php Ajax-一个页面中的多个调用

Php Ajax-一个页面中的多个调用,php,jquery,ajax,Php,Jquery,Ajax,现在我的代码只加载1页(load.php?cid=1,但我想在不同的div中加载5-8页(cid=1,cid=2,cid=3,cid=4,cid=5,cid=6,cid=10…等等) $(document).ready(function() { function loading_show() { $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast')

现在我的代码只加载1页(load.php?cid=1,但我想在不同的div中加载5-8页(cid=1,cid=2,cid=3,cid=4,cid=5,cid=6,cid=10…等等)

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
我将如何实现它

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
$(文档).ready(函数(){
函数加载_show(){
$('#加载分页').html(“”.fadeIn('fast');
}
函数加载_hide(){
$(“#加载#分页”).fadeOut“fast”);
}
函数加载数据(第页){
加载_show();
$.ajax({
类型:“POST”,
url:“load.php?cid=1”,
数据:“第页=”+第页,
成功:功能(msg){
$(“#容器#分页”).ajaxComplete(函数(事件、请求、设置){
加载_hide();
$(“#容器分页”).html(msg);
});
}
});
}

正如JimL所提到的,我会给页面上的每个元素一个公共类,给每个元素一个唯一的数据属性,比如
data cid=“1”
,遍历每个元素,获取cid并为每个元素调用ajax函数

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
我更进一步,使用承诺获取所有ajax响应,然后在承诺得到解决(当所有ajax请求完成时)加载所有数据

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
这是

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
HTML:

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
<div id="loading_Paging"></div>
<div class="myElements" data-cid="1"></div>
<div class="myElements" data-cid="2" ></div>
<div class="myElements" data-cid="3" ></div>
<div class="myElements" data-cid="4" ></div>
<div class="myElements" data-cid="5" ></div>
<div class="myElements" data-cid="6" ></div>
<div class="myElements" data-cid="7"></div>
<div class="myElements" data-cid="8" ></div>
$(function() {

    var page = 'some string...'
    loadData(page);

    function loadData(){
        $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        // loop through each image element
        // calling the ajax function for each and storing the reponses in a `promise`
        var promises = $('.myElements').map(function(index, element) {
            var cid = '&&cid=' + $(this).data('cid'); // get the cid attribute 
            return $.ajax({
                    type: "POST",
                    url: 'load.php',
                    data: "page=" + page +cid, //add the cid info to the post data
                    success: function(msg) {
                    }
            });
        });
        // once all of the ajax calls have returned, te promise is resolved 
        // and the below function is called 
        $.when.apply($, promises).then(function() {
            // arguments[0][0] is first result
            // arguments[1][0] is second result and so on
            for (var i = 0; i < arguments.length; i++) {
                $('.myElements').eq(i).html( arguments[i][0] );
            }
            $('#loading_Paging').fadeOut('fast');
        });
     }
});
<?php
if (isset($_POST['cid']) ){
    // this is just a contrived example
    // in your code youd use the cid to 
    // get whatever data you need for the current div
    echo 'This is returned message '.$_POST['cid'];
}
?>

jQuery:

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
<div id="loading_Paging"></div>
<div class="myElements" data-cid="1"></div>
<div class="myElements" data-cid="2" ></div>
<div class="myElements" data-cid="3" ></div>
<div class="myElements" data-cid="4" ></div>
<div class="myElements" data-cid="5" ></div>
<div class="myElements" data-cid="6" ></div>
<div class="myElements" data-cid="7"></div>
<div class="myElements" data-cid="8" ></div>
$(function() {

    var page = 'some string...'
    loadData(page);

    function loadData(){
        $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        // loop through each image element
        // calling the ajax function for each and storing the reponses in a `promise`
        var promises = $('.myElements').map(function(index, element) {
            var cid = '&&cid=' + $(this).data('cid'); // get the cid attribute 
            return $.ajax({
                    type: "POST",
                    url: 'load.php',
                    data: "page=" + page +cid, //add the cid info to the post data
                    success: function(msg) {
                    }
            });
        });
        // once all of the ajax calls have returned, te promise is resolved 
        // and the below function is called 
        $.when.apply($, promises).then(function() {
            // arguments[0][0] is first result
            // arguments[1][0] is second result and so on
            for (var i = 0; i < arguments.length; i++) {
                $('.myElements').eq(i).html( arguments[i][0] );
            }
            $('#loading_Paging').fadeOut('fast');
        });
     }
});
<?php
if (isset($_POST['cid']) ){
    // this is just a contrived example
    // in your code youd use the cid to 
    // get whatever data you need for the current div
    echo 'This is returned message '.$_POST['cid'];
}
?>
$(函数(){
var page='一些字符串…'
装载数据(第页);
函数loadData(){
$('#加载分页').html(“”.fadeIn('fast');
//循环遍历每个图像元素
//为每个函数调用ajax函数,并将响应存储在“promise”中`
var promises=$('.myElements').map(函数(索引,元素){
var cid='&&cid='+$(this.data('cid');//获取cid属性
返回$.ajax({
类型:“POST”,
url:'load.php',
数据:“page=“+page+cid,//将cid信息添加到post数据中
成功:功能(msg){
}
});
});
//一旦所有ajax调用都返回,te promise就被解决了
//下面的函数被调用
$.when.apply($,promissions).then(function(){
//参数[0][0]是第一个结果
//参数[1][0]是第二个结果,依此类推
for(var i=0;i
我在示例中使用的PHP:

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
<div id="loading_Paging"></div>
<div class="myElements" data-cid="1"></div>
<div class="myElements" data-cid="2" ></div>
<div class="myElements" data-cid="3" ></div>
<div class="myElements" data-cid="4" ></div>
<div class="myElements" data-cid="5" ></div>
<div class="myElements" data-cid="6" ></div>
<div class="myElements" data-cid="7"></div>
<div class="myElements" data-cid="8" ></div>
$(function() {

    var page = 'some string...'
    loadData(page);

    function loadData(){
        $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        // loop through each image element
        // calling the ajax function for each and storing the reponses in a `promise`
        var promises = $('.myElements').map(function(index, element) {
            var cid = '&&cid=' + $(this).data('cid'); // get the cid attribute 
            return $.ajax({
                    type: "POST",
                    url: 'load.php',
                    data: "page=" + page +cid, //add the cid info to the post data
                    success: function(msg) {
                    }
            });
        });
        // once all of the ajax calls have returned, te promise is resolved 
        // and the below function is called 
        $.when.apply($, promises).then(function() {
            // arguments[0][0] is first result
            // arguments[1][0] is second result and so on
            for (var i = 0; i < arguments.length; i++) {
                $('.myElements').eq(i).html( arguments[i][0] );
            }
            $('#loading_Paging').fadeOut('fast');
        });
     }
});
<?php
if (isset($_POST['cid']) ){
    // this is just a contrived example
    // in your code youd use the cid to 
    // get whatever data you need for the current div
    echo 'This is returned message '.$_POST['cid'];
}
?>


“最简单”的解决方案是只复制您正在执行的jquery ajax调用。为了更简洁,您可以对要加载的div使用一些巧妙的命名,对它们进行迭代并执行适当的ajax调用。即给每个“空”div一个可加载(或类似)的类,并给它一个类似于data cid=“x”的属性。然后选择所有$('.loadable'),并为每一个执行一次ajax with cid=data cid。我在下面留下了完整的答案,但仅供参考,在您的原始代码中,您缺少了一个
.fadeOut'fast');
最后(尽管第二个可能刚刚从您的复制/粘贴中被遗漏),这里的问题是我不能发布完整的代码。