$ .Post不第二次触发 [英] $.Post not firing the second time

查看:151
本文介绍了$ .Post不第二次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建关注"按钮,但返回的数据(即关注"按钮)不起作用.

Im trying to make a 'Follow' button but the returned data, which is the 'Unfollow' button, is not working.

$('.follow_button').click(function() {
    //event.preventDefault();
    var visitor_user_id = $('.follow_button').attr('id');

    $('#link_visitor_follow').empty().append('<div id = "follow_jquery_btn"><img src = "css/images/ajax_follow.gif" width = "12" height = "12" /> Follow</div>');
    $.post('/unime/user_follow.php', {'type':'follow_me', visitor_user_id:visitor_user_id}, function(data){
        if(data){
            $('#link_visitor_follow').empty().html(data);
        }
    });
    return false;
});

$('.unfollow_button').click(function() {
    //event.preventDefault();
    var visitor_user_id = $('.unfollow_button').attr('id');
    $('#link_visitor_unfollow').empty().append('<div id = "follow_jquery_btn"><img src = "css/images/ajax_follow.gif" width = "12" height = "12" /> Following</div>');
    $.post('/unime/user_follow.php', {'type':'unfollow_me', visitor_user_id:visitor_user_id}, function(data){
        if(data){
            $('#link_visitor_unfollow').empty().html(data);
        }
    });
        return false;
});

PHP返回的数据:

echo "<a class = 'unfollow_button' id = 'visitor_".$visitor_user_id."'><span id = 'check_mark'></span> Unfollow</a>";

当我单击取消关注"按钮时,尽管有代码设置,但它不起作用. PHP本身没有错.单击取消关注时,它甚至都没有调用Ajax.

When I click the Unfollow button, it is not working, although I have the code setup for it. There is nothing wrong with the PHP itself. Its not even calling Ajax when I click Unfollow.

推荐答案

您只能绑定到当前存在的元素.如果不存在,则需要将其委托给将存在的元素.

You can only bind to elements that currently exist. If they do not, you need to delegate to the element that WILL exist.

更改:

$('.unfollow_button').click(function() {

收件人:

$('#link_visitor_follow').on('click', '.unfollow_button', function() {

它将单击分配给尚不存在的元素,该事件将从取消关注按钮冒泡,直到它到达绑定了事件的link_visitor_follow为止,由于它来自取消关注按钮,因此现在将调用该事件(如果存在很有道理.)

And it will delegate clicks to the not yet existent element, the event will bubble up from unfollow button until it hits link_visitor_follow, which has an event bound, and since it came from unfollow button it will now call the event (if that makes sense).

此外,如果它们跟随,取消跟随,然后再次跟随,则您需要对跟随"按钮执行相同的操作.

Also, you will need to do the same thing for the follow button in case they follow, unfollow, then follow again.

这篇关于$ .Post不第二次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆