Ajax jquery success scope

后端 未结 3 1338
执笔经年
执笔经年 2020-11-27 07:02

I have this ajax call to a doop.php.

    function doop(){
        var old = $(this).siblings(\'.old\').html();
        var new = $(this).siblin         


        
3条回答
  •  隐瞒了意图╮
    2020-11-27 07:41

    First of all new is a reserved word. You need to rename that variable.

    To answer your question, Yes, you need to save this in a variable outside the success callback, and reference it inside your success handler code:

    var that = this;
    $.ajax({
        // ...
        success: function(resp) {
            if(resp == 1) {
                $(that).siblings('.old').html($new);
            }
        }
    })
    

    This is called a closure.

提交回复
热议问题