jQuery绑定在AJAX调用后单击一个链接

时间:2022-06-25 20:37:15

I'm getting furious - perhaps someone will be able to help me with this.

我很生气 - 也许有人能够帮助我。

I need to re-bind the click to the link after AJAX call, but for some reason it doesn't want to work.

我需要在AJAX调用后将点击重新绑定到链接,但由于某种原因它不想工作。

Here's my code:

这是我的代码:

if ($('.active').length > 0) {
    $('.active').click(function() {
        var elem = $(this);
        var url = $(this).attr('href');
        $.ajax({
            url: url,
            dataType: 'html',
            success: function(data) {
                elem.replaceWith(data);                                                     
            }       
        });         
        $('.active').bind('click'); return false;           
    });
}

Any idea?

任何想法?

Thanks for the responses - I've amended the code, but the problem is still there:

感谢您的回复 - 我修改了代码,但问题仍然存在:

function makeActive() {
    if ($('.active').length > 0) {
        $('.active').click(function() {
            var elem = $(this);
            var url = $(this).attr('href');
            $.ajax({
                url: url,
                dataType: 'html',
                success: function(data) {
                    elem.replaceWith(data);                             
                }       
            }); 
            $('.active').live('click', makeActive);     
            return false;           
        });
    }
}


$('.active').live('click', makeActive);

2 个解决方案

#1


21  

You would have to add the rebinding in the success handler if you want to execute it after the Ajax call:

如果要在Ajax调用之后执行它,则必须在成功处理程序中添加重新绑定:

success: function(data) {
    elem.replaceWith(data);
    $('.active').bind('click', /* some function needs to go here*/);
}

That said, in this case, live() or delegate() are probably better options [update: now that jQuery 1.7 is out, everything can be done with .on()]. This would also prevent double assignment of click handlers, in case you have other .active links that have not been replaced.

也就是说,在这种情况下,live()或delegate()可能是更好的选择[更新:现在jQuery 1.7已经完成,一切都可以用.on()完​​成)。如果您有其他尚未替换的.active链接,这也可以防止点击处理程序的双重分配。

Update: Regarding your updated code: The way you are using live defeats its purpose. Please read its documentation. What you are doing is assigning a click handler when the the link is clicked, which means that you are adding click handlers over and over again.

更新:关于您的更新代码:您使用实时的方式失败了它的目的。请阅读其文档。您正在做的是在单击链接时分配单击处理程序,这意味着您反复添加单击处理程序。

This is an improved version of your code.

这是代码的改进版本。

$('.active').live('click', function(event) {
    var elem = $(this);
    var url = $(this).attr('href');
     $.ajax({
         url: url,
         dataType: 'html',
         success: function(data) {
              elem.replaceWith(data);                             
         }       
     });    
     event.preventDefault();
     event.stopPropagation();
});

#2


31  

UPDATE on October 31, 2012

更新于2012年10月31日

Starting from jQuery 1.7, the recommended approach is to use on -

从jQuery 1.7开始,推荐的方法是使用 -

$(document).on('click', '.active', function () {
    // click handler code goes here
});

Can you try the following ?

你可以试试以下吗?

$('.active').live('click', function()
{
    // click handler
});

#1


21  

You would have to add the rebinding in the success handler if you want to execute it after the Ajax call:

如果要在Ajax调用之后执行它,则必须在成功处理程序中添加重新绑定:

success: function(data) {
    elem.replaceWith(data);
    $('.active').bind('click', /* some function needs to go here*/);
}

That said, in this case, live() or delegate() are probably better options [update: now that jQuery 1.7 is out, everything can be done with .on()]. This would also prevent double assignment of click handlers, in case you have other .active links that have not been replaced.

也就是说,在这种情况下,live()或delegate()可能是更好的选择[更新:现在jQuery 1.7已经完成,一切都可以用.on()完​​成)。如果您有其他尚未替换的.active链接,这也可以防止点击处理程序的双重分配。

Update: Regarding your updated code: The way you are using live defeats its purpose. Please read its documentation. What you are doing is assigning a click handler when the the link is clicked, which means that you are adding click handlers over and over again.

更新:关于您的更新代码:您使用实时的方式失败了它的目的。请阅读其文档。您正在做的是在单击链接时分配单击处理程序,这意味着您反复添加单击处理程序。

This is an improved version of your code.

这是代码的改进版本。

$('.active').live('click', function(event) {
    var elem = $(this);
    var url = $(this).attr('href');
     $.ajax({
         url: url,
         dataType: 'html',
         success: function(data) {
              elem.replaceWith(data);                             
         }       
     });    
     event.preventDefault();
     event.stopPropagation();
});

#2


31  

UPDATE on October 31, 2012

更新于2012年10月31日

Starting from jQuery 1.7, the recommended approach is to use on -

从jQuery 1.7开始,推荐的方法是使用 -

$(document).on('click', '.active', function () {
    // click handler code goes here
});

Can you try the following ?

你可以试试以下吗?

$('.active').live('click', function()
{
    // click handler
});