简体   繁体   中英

jquery not working inside if after AJAX success/failure

I have just started learning AJAX Before posting I have already searched and refered to previous asked questions like this but it didn't helped well My Code is:

$('.del').click(function(){
    $(this).parent().hide('slow');
    $.get( "index.php", {del:$(this).attr('id')}).done(function(data){
        if (data==='1') {
            $(this).parent().remove();
        }
        else{
            $(this).parent().show('slow');
        }
    });
});

the jquery statements inside if as well as else is not executed To verify every thing is going well I had added an alert in the statements and it popped up successfully but the jquery statements isn't working.Whats The problem and how to fix this ?

I believe your $(this) reference is incorrect in that scope. Try:

$('.del').click(function(){
    var $this = $(this);
    $this.parent().hide('slow');
    $.get( "index.php", {del:$this.attr('id')}).done(function(data){
        if (data==='1') {
            $this.parent().remove();
        }
        else{
            $this.parent().show('slow');
        }
    });
});

I think it may be that your 'this' variable is out of scope in the callback. Try binding it, or use jQuery's proxy() feature

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