Ajax jquery success scope

后端 未结 3 1334
执笔经年
执笔经年 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:37

    You should use the context setting as in http://api.jquery.com/jQuery.ajax/

    function doop(){
        var old = $(this).siblings('.old').html();
        var newValue = $(this).siblings('.new').val();
    
        $.ajax({
            url: 'doop.php',
            type: 'POST',
            context: this,
            data: 'before=' + old + '&after=' + newValue,
            success: function(resp) {
                if(resp == 1) {
                    $(this).siblings('.old').html(newValue);
                }
            }
        });
    
        return false;
    }
    

    "this" will be transfer to the success scope and will act as expected.

提交回复
热议问题