ajax成功回调在$ .each中不起作用

时间:2013-11-20 01:50:55

标签: php jquery ajax json

我在$ .each语句中有一个ajax帖子,数据正确发布到PHP,但成功回调无法正常工作

下面是我的jquery。该问题从“// rate videos”评论下方开始。同样,所有数据都正确地发送到PHP,只是回调在这里不起作用。

$(document).ready(function() {
    //get user info for header
    $.getJSON('/dev/new/scripts/get_user_info.php', function(user) {
        $(".signout").append('welcome ' + user.first_name + '<br/><a href="/dev/new/scripts/logout.php">SIGN OUT</a>');
        console.log(user);
     });

     //display videos and video ratings
     $.getJSON('/dev/new/scripts/get_vibrary_alleyes.php', function(videos) {
        $.each(videos.vibrary_videos_alleyes, function(name,value) {
            var vibrary_html = '<div class="alleyes-video-row"><div class="video-user"><img src="' + value.profile_url + '" width="77px" height="auto"><br/>' + value.first_name + '</div><div class="alleyes-right"><div class="alleyes-title">' + value.video_title + '</div><div class="alleyes-date">' + value.upload_date + '</div><div class="alleyes-video"><img src="' + value.video_url + '" width="347px" height="auto"></div><div class="alleyes-rate-reply" id="video_' + value.video_id + '"></div></div></div>';
            $(".vibrary-videos").append(vibrary_html);
            if (value.avg_rating <= 0) {
                $("#video_" + value.video_id).append('<div class="alleyes-rating-row"><div class="alleyes-stars"><img class="rate-1-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="rate-2-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="rate-3-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="rate-4-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="pad-right-10 rate-5-' + value.video_id + '" src="/dev/new/images/rate_g.png"></div>Rate this video</div><div class="alleyes-reply-row"><img src="/dev/new/images/reply_video_icon.png" class="pad-right-10">Reply by video</div>');
            }
            if (value.avg_rating > 0 && value.avg_rating < 1.5) {
                $("#video_" + value.video_id).append('<div class="alleyes-rating-row"><div class="alleyes-stars"><img class="rate-1-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-2-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="rate-3-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="rate-4-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="pad-right-10 rate-5-' + value.video_id + '" src="/dev/new/images/rate_g.png"></div>Rate this video</div><div class="alleyes-reply-row"><img src="/dev/new/images/reply_video_icon.png" class="pad-right-10">Reply by video</div>');
            }
            if (value.avg_rating >= 1.5 && value.avg_rating < 2.5) {
                $("#video_" + value.video_id).append('<div class="alleyes-rating-row"><div class="alleyes-stars"><img class="rate-1-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-2-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-3-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="rate-4-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="pad-right-10 rate-5-' + value.video_id + '" src="/dev/new/images/rate_g.png"></div>Rate this video</div><div class="alleyes-reply-row"><img src="/dev/new/images/reply_video_icon.png" class="pad-right-10">Reply by video</div>');
            }
            if (value.avg_rating >= 2.5 && value.avg_rating < 3.5) {
                $("#video_" + value.video_id).append('<div class="alleyes-rating-row"><div class="alleyes-stars"><img class="rate-1-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-2-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-3-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-4-' + value.video_id + '" src="/dev/new/images/rate_g.png"><img class="pad-right-10 rate-5-' + value.video_id + '" src="/dev/new/images/rate_g.png"></div>Rate this video</div><div class="alleyes-reply-row"><img src="/dev/new/images/reply_video_icon.png" class="pad-right-10">Reply by video</div>');
            }
            if (value.avg_rating >= 3.5 && value.avg_rating < 4.5) {
                $("#video_" + value.video_id).append('<div class="alleyes-rating-row"><div class="alleyes-stars"><img class="rate-1-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-2-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-3-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-4-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="pad-right-10 rate-5-' + value.video_id + '" src="/dev/new/images/rate_g.png"></div>Rate this video</div><div class="alleyes-reply-row"><img src="/dev/new/images/reply_video_icon.png" class="pad-right-10">Reply by video</div>');
            }
            if (value.avg_rating >= 4.5) {
                $("#video_" + value.video_id).append('<div class="alleyes-rating-row"><div class="alleyes-stars"><img class="rate-1-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-2-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-3-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="rate-4-' + value.video_id + '" src="/dev/new/images/rate_y.png"><img class="pad-right-10 rate-5-' + value.video_id + '" src="/dev/new/images/rate_y.png"></div>Rate this video</div><div class="alleyes-reply-row"><img src="/dev/new/images/reply_video_icon.png" class="pad-right-10">Reply by video</div>');
            }

            //rate videos
            $(document).on('click', ".rate-1-" + value.video_id, function() {
                $.ajax({
                    type: 'post',
                    url: '/dev/new/scripts/rate_video.php',
                    dataType: 'json',
                    data: {video_id:value.video_id,rating:1},
                    success: function () {
                        alert('sdfdas');
                    }
                });

            });
            $(document).on('click', ".rate-2-" + value.video_id, function() {
                $.ajax({
                    type: 'post',
                    url: '/dev/new/scripts/rate_video.php',
                    dataType: 'json',
                    data: {video_id:value.video_id,rating:2},
                    success: function () {
                        alert('sdfdas');
                    }
                });

            });
            $(document).on('click', ".rate-3-" + value.video_id, function() {
                $.ajax({
                    type: 'post',
                    url: '/dev/new/scripts/rate_video.php',
                    dataType: 'json',
                    data: {video_id:value.video_id,rating:3},
                    success: function () {
                        alert('sdfdas');
                    }
                });

            });
            $(document).on('click', ".rate-4-" + value.video_id, function() {
                $.ajax({
                    type: 'post',
                    url: '/dev/new/scripts/rate_video.php',
                    dataType: 'json',
                    data: {video_id:value.video_id,rating:4},
                    success: function () {
                        alert('sdfdas');
                    }
                });

            });
            $(document).on('click', ".rate-5-" + value.video_id, function() {
                $.ajax({
                    type: 'post',
                    url: '/dev/new/scripts/rate_video.php',
                    dataType: 'json',
                    data: {video_id:value.video_id,rating:5},
                    success: function () {
                        alert('sdfdas');
                    }
                });

            });

        });
        console.log(videos);
     });

});

1 个答案:

答案 0 :(得分:1)

我认为你想使用jQuery on方法。

$(document).on('click', ".rate-1-" + value.video_id, function() {
    // ...
});

如果您使用的是低于1.7的jQuery版本,则需要使用live