Accessing $(this) after success:function() when using $.ajax

前端 未结 3 2043
情话喂你
情话喂你 2021-01-23 07:14

How can I access the value of $(this) after success: function() when using jquery? No matter what I\'ve tried, it appears I cant do it.

$(\'.add\').click(functio         


        
3条回答
  •  生来不讨喜
    2021-01-23 07:46

    "this" inside your ajax success is not the same "this" outside.

    Different scopes...

    There is a nice solution for this question.

    $('.add').click(function() {
    var that = $(this);
    //do whatever you want :]
    
     $.ajax({
            type:"GET",
            url:"/",
            data:data,
            dataType: 'json',
            beforeSend:function(html){
                //Nothing here right now
            },
            success: function(){
                that.parent("div").after("
    I'm the new one.
    "); }, }); });

提交回复
热议问题