简体   繁体   中英

Jquery function doesn't work after Ajax call

I've got this function:

$(document).ready(function() {
$('.post_button, .btn_favorite').click(function() {


//Fade in the Popup
$('.login_modal_message').fadeIn(500);

// Add the mask to body
$('body').append('<div class="overlay"></div>');
$('.overlay').fadeIn(300);  
return false;
});

My page loads content with favourite buttons, but after Ajax call and generated additional new content the function doesn't work when you click new content's buttons. What could be not right?

That is because you are using dynamic content.

You need to change your click call to a delegated method like on

$('.post_button, .btn_favorite').on('click', function() {

or

$("body").on( "click", ".post_button, .btn_favorite", function( event ) {

Instead of this:

$('.post_button, .btn_favorite').click(function() {

do this:

$(document).on('click','.post_button, .btn_favorite', function() {

on will work with present elements and future ones that match the selector.

Cheers

class-of-element is the applied class of element. which is selector here.

  $(document).on("click", ".class-of-element", function (){
    alert("Success");
  });

I am not sure if I am getting your question right but you may want to try..

$.ajax({
  url: "test.html"
}).done(function() {
   $('.post_button, .btn_favorite').click(function() {


    //Fade in the Popup
    $('.login_modal_message').fadeIn(500);

   // Add the mask to body
   $('body').append('<div class="overlay"></div>');
   $('.overlay').fadeIn(300);  
   return false;
});

Just try to paste your code inside done function.

Hope it helps :)

EDIT: I also notice you are missing }); on your question.

If you know the container for .post_button, .btn_favorite then use

$('#container_id').on('click', '.post_button, .btn_favorite', function () { });

so if '.post_button, .btn_favorite' are not found then it will bubble up to container_id

else if you don't know the container then delegate it to document

$(document).on('click', '.post_button, .btn_favorite', function () { });

Reference

The following worked for me

    $(document).ready(function(){ 
         $(document).bind('contextmenu', function(e) {
            if( e.button == 2 && jQuery(e.target).is('img')) {
              alert('These photos are copyrighted by the owner. \nAll rights reserved. \nUnauthorized use prohibited.'); 
              return false;
            }
         });
    });

You need to bind the jQuery click event once your ajax content is replaced old content

in AJAX success block you need to add code like here new response html content one a tag like

<a href="#" id="new-tag">Click Me</a>

So you can bind the new click event after change the content with following code

$("#new-tag").click(function(){
   alert("hi");
   return false;
});

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