在AJAX jQuery load()之后运行jQuery代码

时间:2015-03-17 09:42:13

标签: javascript jquery ajax

只有在代码1从外部页面加载了所有内容后,我才能运行以下代码?

jQuery Script尝试了

//filters
$(window).bind("load", function() {
    $(".filters li").on("click", function () {
    id = ($(this).data("id")+'').split(',');
    filter = $(this).data("filter");
    $("#hotel-list .box").hide();
    id[0] == "all" && $("#hotel-list .box").show() || id.forEach(function(v){
        $('#hotel-list .box[data-'+filter+'*="'+v.trim()+'"]').show();
    });
    return false;
});
      <!-- Count Star Rating -->
        var two_stars = $("article[data-stars*='2']").length;
        var three_stars = $("article[data-stars*='3']").length;
        var four_stars = $("article[data-stars*='4']").length;
        var five_stars = $("article[data-stars*='5']").length;
        $('.total-three').text(three_stars);
        $('.total-four').text(four_stars);
        $('.total-five').text(five_stars);
        var totals = three_stars + four_stars + five_stars + two_stars;
        $('.totals').text(totals);      
        <!-- Count Board -->
        var board_no = $("article[data-board*='No']").length    
        var board_ro = $("article[data-board*='Room']").length  
        var board_bb = $("article[data-board*='Breakfast']").length;            
        var board_fb = $("article[data-board*='Full Board']").length    
        var board_hb = $("article[data-board*='Half']").length  
        var board_ai = $("article[data-board*='All']").length   
        var board_sc = $("article[data-board*='Self']").length  
        $('.total-ro').text(board_ro);
        $('.total-bb').text(board_bb);
        $('.total-fb').text(board_fb);
        $('.total-hb').text(board_hb);
        $('.total-ai').text(board_ai);
        $('.total-sc').text(board_sc);
        var total = board_ro + board_bb + board_fb + board_hb + board_ai + board_sc + board_no;
        $('.total').text(total);

  //Fix broken images
  fixBrokenImages = function( url ){
    var img = document.getElementsByTagName('img');
    var i=0, l=img.length;
    for(;i<l;i++){
        var t = img[i];
        if(t.naturalWidth === 0){
//this image is broken
            t.src = url;
        }
    }
}
 window.onload = function() {
    fixBrokenImages('images/noimg.png');
 }
});

jQuery AJAX代码是:

var datastring = location.search; // now you can update this var and use
$("#hotel-list").load("Rezults2.php"+ datastring + " #hotel-list> *");

所以我需要第一个代码只在第二个代码完成后才能执行(从3到9秒)

关于这个问题?

2 个答案:

答案 0 :(得分:2)

function runThisAfter() {
    var two_stars = $("article[data-stars*='2']").length;
    var three_stars = $("article[data-stars*='3']").length;
    var four_stars = $("article[data-stars*='4']").length;
    var five_stars = $("article[data-stars*='5']").length;
    //etc...
}

$("#hotel-list").load("Rezults2.php", function(){ runThisAfter(); });

当加载完成时,这将运行“runThisAfter()”。

答案 1 :(得分:0)

解决方案是:

$(document).ajaxComplete(function( event, xhr, settings ) {
   // Your complete function here
});

或者在我的案例中完整代码:

        $(document).ajaxComplete(function(event, xhr, settings) {
        //Count STARS
        var two_stars = $("article[data-stars*='2']").length;
        var three_stars = $("article[data-stars*='3']").length;
        var four_stars = $("article[data-stars*='4']").length;
        var five_stars = $("article[data-stars*='5']").length;
        $('.total-three').text(three_stars);
        $('.total-four').text(four_stars);
        $('.total-five').text(five_stars);
        var totals = three_stars + four_stars + five_stars +
          two_stars;
        $('.totals').text(totals);
        //COUNT BOARD
        var board_no = $("article[data-board*='No']").length
        var board_ro = $("article[data-board*='Room']").length
        var board_bb = $("article[data-board*='Breakfast']").length;
        var board_fb = $("article[data-board*='Full Board']").length
        var board_hb = $("article[data-board*='Half']").length
        var board_ai = $("article[data-board*='All']").length
        var board_sc = $("article[data-board*='Self']").length
        $('.total-ro').text(board_ro);
        $('.total-bb').text(board_bb);
        $('.total-fb').text(board_fb);
        $('.total-hb').text(board_hb);
        $('.total-ai').text(board_ai);
        $('.total-sc').text(board_sc);
        var total = board_ro + board_bb + board_fb + board_hb +
          board_ai + board_sc + board_no;
        $('.total').text(total);
        //Fix broken images
        fixBrokenImages = function(url) {
          var img = document.getElementsByTagName('img');
          var i = 0,
            l = img.length;
          for (; i < l; i++) {
            var t = img[i];
            if (t.naturalWidth === 0) {
              //this image is broken
              t.src = url;
            }
          }
        }
        window.onload = function() {
          fixBrokenImages('images/noimg.png');
        }
        });
        });