在jQuery的AJAX调用的外部访问数据 [英] Accessing data outside of ajax call in jQuery

查看:627
本文介绍了在jQuery的AJAX调用的外部访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code我的工作,我的目标是调用Ajax来返回一些数据,并追加按钮/ $(本)被点击。

  $('。click_me)。点击(函数(){

    $阿贾克斯({
        键入:POST,
        网址:AJAX / get_list.php
    })。完成(功能(数据){
        $(本).append(数据);
    });
});
 

解决方案

$就返回一个XHR对象,它是被调用的方法完成的背景下。因此,你需要存储按钮的情况下先使AJAX之前使用该变量。

  $('。click_me)。点击(函数(){
    变量$自我= $(本);
    $阿贾克斯({
        键入:POST,
        网址:AJAX / get_list.php
    })。完成(功能(数据){
        $ self.append(数据);
    });
});
 

Below is the code I am working on, my goal is to call the ajax to return some data and append that data on the button/$(this) that is clicked.

$('.click_me').click(function(){

    $.ajax({
        type: 'POST',
        url: 'ajax/get_list.php'
    }).done(function(data){
        $(this).append(data);
    });
});

解决方案

$.ajax returns an XHR object and it is the context which is calling the done method. Hence you need to store the context of button first before making the ajax and use that variable.

$('.click_me').click(function(){
    var $self = $(this);
    $.ajax({
        type: 'POST',
        url: 'ajax/get_list.php'
    }).done(function(data){
        $self.append(data);
    });
});

这篇关于在jQuery的AJAX调用的外部访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆