简体   繁体   中英

Bootstrap slide event not working inside AJAX success

This is my code, 'slide' event inside AJAX success call.

success: function(data) {
    $('#my_container').html(data);
    // I am getting' #mycarousel' successfully in to #my_container
    $('#mycarousel').bind('slid', function(e) {
        console.log('hi');
    }
    // tried 'slid.bs.carousel' too, no change.
    // btw, I can see my 'slid' function under event listeners for that element.
    // when I paste the above binding code in console again it shows the element too.
});

I want to print the 'hi' to console on slid event, which is not working now.

Thanks

After loading the carousel dynamically, you have to initialize it (as androbin suggested):

  $('#my_container').html(data);

  $("#mycarousel").carousel();

  $('#mycarousel').bind('slide.bs.carousel', function (e) {
      console.log('slide event!');
  });

  $('#mycarousel').bind('slid', function (e) {
      console.log("slid event!");
  });

Here you can see it working: http://jsfiddle.net/faw242a8/1/

Make sure the html you are pulling in via ajax contains a valid carousel.

Reference: Carousel - Bootstrap

Check whether you have multiple versions of jquery libraries loaded in the page.

Use $.fn.jquery to find the loaded version and cross check it with the jquery version you have included and see both are same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM