简体   繁体   English

我如何获取jQuery Ajax成功回调中的“ clicked”元素

[英]How can I get in 'clicked' element in jquery ajax success callback

This is one easy (I hope) question which is bugging me for years. 这是一个容易(我希望)的问题,困扰了我很多年。 How do you get element that fired event which triggered the ajax request? 如何获取触发ajax请求的事件触发元素? Here is an example: 这是一个例子:

<button id="clickme">Click me</button>
$("#clickme").click(function(e){
    var i_need_this=e.target;
    alert(i_need_this); //nice!
    $.ajax({
            url:'http://echo.jsontest.com/',

            type: 'GET',
            success: function( data, status, jqXHR  ) {
                alert('success');
              //console.log( ??? ); Get i_need_this from somewhere?       
            }
        });

})

Live: http://jsfiddle.net/8F3u2/ 直播: http//jsfiddle.net/8F3u2/

Thanks in advance! 提前致谢!

You can use context param here: 您可以在此处使用context参数:

$.ajax({
        url:'http://echo.jsontest.com/',
        context:this, // <------this is the cliked button
        type: 'GET',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM