简体   繁体   English

如果AJAX成功/失败后,jQuery无法在内部运行

[英]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: 我刚开始学习AJAX张贴我已经搜查,refered像以前问问题之前这个 ,但它并没有帮助以及我的代码是:

$('.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 ? 内部的jquery语句if执行以及要执行else操作为了验证一切正常,我在语句中添加了一个alert ,该alert成功弹出,但是jquery语句不起作用。这是什么问题以及如何解决此问题?

I believe your $(this) reference is incorrect in that scope. 我相信您的$(this)参考在该范围内不正确。 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. 我认为您的'this'变量可能超出了回调的范围。 Try binding it, or use jQuery's proxy() feature 尝试绑定它,或使用jQuery的proxy() 功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM