1

したがって、ユーザーが [その他] をクリックすると、データベースからより多くのコメントを読み込む単純な JavaScript があります。ここで、このスクリプトを拡張して、ユーザーがコメントを表示できるようにする前に、最初に SQL データベースにデータを入力するようにします。そして、私は正しい軌道に乗っていると感じていますが、それを機能させることはできません.

まず、動作するコード。

$(function() {

$('.load_more').live("click",function() {


    var photoid = document.getElementById('photoid').value;
    var lastid = document.getElementById('lastid').value;


    if(lastid!='end'){

        $.ajax({
        type: "POST",
        url: "/more_comments_ajax.php",
        data: {
            photoid : photoid,
            lastid : lastid
        },
        beforeSend:  function() {
            $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

        },
        success: function(html){//html = the server response html code
            $("#more").remove();//Remove the div with id=more 
            $("div#updates").append(html);//Append the html returned by the server .


        }
        });

    }
    return false;
});
});

今は、このように展開できるはずだと感じています。

$(function() {
        $.ajax({
        type: "POST",
        url: "/populate_sql.php",

        beforeSend:  function() {
            $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request
        },

    sucess: $('.load_more').live("click",function() {


    var photoid = document.getElementById('photoid').value;
    var lastid = document.getElementById('lastid').value;


    if(lastid!='end'){

        $.ajax({
        type: "POST",
        url: "/more_comments_ajax.php",
        data: {
            photoid : photoid,
            lastid : lastid
        },
        beforeSend:  function() {
            $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

        },
        success: function(html){//html = the server response html code
            $("#more").remove();//Remove the div with id=more 
            $("div#updates").append(html);//Append the html returned by the server .


        }
        });

    }
    return false;
});
});
});

どこでそれを失っていますか?

4

2 に答える 2

1

この機能を使用できます。私は以前にそれを使用しました、そしてそれは素晴らしい働きをします。最初のコールバックが受信した後、2番目のリクエストを送信します。

(function($) 
    {
        var ajaxQueue = $({});
        $.ajaxQueue = function(ajaxOpts) 
        {
            var oldComplete = ajaxOpts.complete;
            ajaxQueue.queue(function(next)
            {
                ajaxOpts.complete = function() 
                {
                    if (oldComplete) oldComplete.apply(this, arguments);
                    next();
                };
                $.ajax(ajaxOpts);
            });
        };
    })(jQuery);

通常のajaxのように使用してください。サンプル:

      $.ajaxQueue({ url: 'x.php', data:{x:x,y:y}, type: 'POST', 
        success: function(respond) 
        {
            .....
        }
        });

したがって、最初のajaxからのコールバックがあったかどうかを確認してから、2番目のリクエストを送信できます。お役に立てば幸いです。

于 2012-08-28T15:32:42.230 に答える
1

答えてくれてありがとう。それはまさに私が必要としていたものではありませんでしたが、それは私にアイデアを与え、私にとってうまくいく解決策は一緒に働く2つのjavascriptでした. 誰かが同様のものが必要な場合は、ここにコードを残します。

<script type="text/javascript">
jQuery(function($){
var pid = '<?php echo $ids['0']; ?>';
$.ajax({
type: "POST",
url: "/prepare_sql.php",
data: "pid="+ pid,
beforeSend:  function() {
        $('div#updates').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

    },
    success: function(html) {
        $("div#updates").replaceWith(html);
    }
});
});
</script>


<script type="text/javascript">
$('.load_more').live("click",function() {


var photoid = document.getElementById('photoid').value;
var lastid = document.getElementById('lastid').value;


if(lastid!='end'){

    $.ajax({
    type: "POST",
    url: "/more_comments_ajax.php",
    data: {
        photoid : photoid,
        lastid : lastid
    },
    beforeSend:  function() {
        $('a.load_more').html('<img src="/images/loading.gif" />');//Loading image during the Ajax Request

    },
    success: function(html){//html = the server response html code
        $("#more").remove();//Remove the div with id=more 
        $("div#updates").append(html);//Append the html returned by the server .


    }
    });

}
return false;
});

</script>
于 2012-08-28T20:05:08.933 に答える