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

前端 未结 3 2058
情话喂你
情话喂你 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:24

    Save this to a variable before making the ajax call and use that instead:

    $('.add').click(function() {
        var self = this; // save this as self
        //etc etc
        $.ajax({
                type:"GET",
                url:"/",
                data:data,
                dataType: 'json',
                beforeSend:function(html){
                    //Nothing here right now
                },
                success: function(){
                    // use self instead of this
                    $(self).parent("div").after("
    I'm the new one.
    "); },

提交回复
热议问题