$('#list li a').on('click', function(e) {
    var user_id = this.parentNode.id.replace('list_', '');
    var id = 'id=' + user_id;
    e.preventDefault();

    $.ajax({
        type: "POST",
        url: "xxx.php",
        data: id,
        success: function() {
            $(this).parent().hide(); //the problem is here
            $('.updateNumber').html(function() {
                return parseInt($(this).text(), 10) + 1;
            });
        }
    });
});

我认为在ajax调用之后,由于(THIS)选择器,它无法识别li列表,未正确引用它,谢谢您的帮助

最佳答案

您是正确的,在Ajax回调this中不是单击的锚。以下应该工作:

    $('#list li a').on('click', function(e) {

        var user_id = this.parentNode.id.replace('list_', '');
        var id = 'id=' + user_id;
        e.preventDefault();

        var $a = $(this);

        $.ajax({
        type: "POST",
        url: "xxx.php",
        data: id,
        success: function(){
            $a.parent().hide();
            $('.updateNumber').html(function(){
               return parseInt($(this).text(),10)+1;
            });
        });
    });


保存对$.ajax()调用之外单击的链接的引用。回调函数可以访问其包含范围内的变量。

09-10 10:57
查看更多