Ajax (this) not working

后端 未结 2 937
清歌不尽
清歌不尽 2021-01-11 19:35

When attempting to access the \'.box\' class of the $container, using (this) inside of the ajax call doesn\'t work.

$container.on(
        \"click\",
               


        
相关标签:
2条回答
  • 2021-01-11 20:07

    You're close. 'this' in the context you are using it, refers to the ajax request rather than the thing that issued the event. To fix this, store a copy of this before making the ajax request:

                       }else{
                            var me = this;
                            $.ajax({
                                ...
                                success: function(data){
                                    description = data[0];
                                    $(me).css('width', '70%');
    
    0 讨论(0)
  • 2021-01-11 20:28

    Just add this to your $.ajax call...

    context: this,
    

    ...and it'll work.


    $.ajax({
        context: this, // <-- right here
        url:'scripts/php/fetchResultsData.php',
        data: {action:value},
        type: 'post',
        dataType: 'json',
        success: function(data) { // ...
    

    0 讨论(0)
提交回复
热议问题