有没有办法为一个jQuery的ajax成功函数来访问它包含的对象吗? [英] Is there a way for a JQuery ajax success function to access the object it's contained in?

查看:146
本文介绍了有没有办法为一个jQuery的ajax成功函数来访问它包含的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JavaScript是这样的:

I have javascript like this:

function Cat() { 

  this.meow = function() { // meow };

  $.ajax( do AJAX call, success: this.meow(); );

}

var TopCat = new Cat();

这不起作用,因为这个使得成功的功能的情况下没有意义。是否有一个优雅的解决方案?

This does not work because 'this' makes no sense in the context of the success function. Is there an elegant solution?

推荐答案

您正在寻找的上下文参数添加到 AJAX 方法。
它允许你设置的所有回调将被称为上下文。

You're looking for the context parameter to the ajax method.
It allows you to set the context in which all callbacks will be called.

function Cat() { 
    this.meow = function() { // meow };
    $.ajax({
        context: this, 
        success: function() { this.meow(); } 
    });    
}

这篇关于有没有办法为一个jQuery的ajax成功函数来访问它包含的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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