简体   繁体   English

Ajax jquery成功范围

[英]Ajax jquery success scope

I have this ajax call to a doop.php . 我有一个doop.php ajax调用。

    function doop(){
        var old = $(this).siblings('.old').html();
        var new = $(this).siblings('.new').val();

        $.ajax({
            url: 'doop.php',
            type: 'POST',
            data: 'before=' + old + '&after=' + new,
            success: function(resp) {
                if(resp == 1) {
                    $(this).siblings('.old').html(new);
                }
            }
        });

        return false;
    }

My problem is that the $(this).siblings('.old').html(new); 我的问题是$(this).siblings('.old').html(new); line isn't doing what it's supposed to do. line没有做它应该做的事情。

thanks.. all helpful comments/answers are voted up. 谢谢..所有有用的评论/答案都被投了票。

Update: it appears that half of the problem was the scope (thanks for the answers that helped me clarify that), but the other half is that I'm trying to use ajax in a synchronous manner. 更新:似乎问题的一半是范围(感谢帮助我澄清的答案),但另一半是我试图以同步方式使用ajax。 I've created a new post 我创建了一个新帖子

You should use the context setting as in http://api.jquery.com/jQuery.ajax/ 您应该使用http://api.jquery.com/jQuery.ajax/中上下文设置

function doop(){
    var old = $(this).siblings('.old').html();
    var newValue = $(this).siblings('.new').val();

    $.ajax({
        url: 'doop.php',
        type: 'POST',
        context: this,
        data: 'before=' + old + '&after=' + newValue,
        success: function(resp) {
            if(resp == 1) {
                $(this).siblings('.old').html(newValue);
            }
        }
    });

    return false;
}

"this" will be transfer to the success scope and will act as expected. “这个”将转移到成功范围,并将按预期行事。

First of all new is a reserved word . 首先, new保留字 You need to rename that variable. 您需要重命名该变量。

To answer your question, Yes, you need to save this in a variable outside the success callback, and reference it inside your success handler code: 要回答你的问题,是的,你需要保存this在成功回调外的变量,并引用它的成功处理程序代码中:

var that = this;
$.ajax({
    // ...
    success: function(resp) {
        if(resp == 1) {
            $(that).siblings('.old').html($new);
        }
    }
})

This is called a closure . 这称为闭包

this is bound to the object to which the executing function was applied. this绑定到应用执行函数的对象。 That could be some AJAX response object, or the global object ( window ), or something else (depending on the implementation of $.ajax . 这可能是一些AJAX响应对象,或全局对象( window ),或其他东西(取决于$.ajax的实现。

Do I need to capture $(this) into a variable before entering the $.ajax call, and then pass it as a parameter to the $.ajax call? 在进入$ .ajax调用之前,是否需要将$(this)捕获到变量中,然后将其作为参数传递给$ .ajax调用? or do I need to pass it to the anonymous success function? 或者我是否需要将其传递给匿名成功函数? If that's going to solve the problem, where do I pass it to the $.ajax? 如果这将解决问题,我将在哪里将其传递给$ .ajax?

You do indeed need a way to capture the value of this before defining the success function. 你确实需要一种方法来捕捉值this定义之前success功能。 Creating a closure is the way to do this. 创建闭包是实现此目的的方法。 You need to define a separate variable (eg self ): 您需要定义一个单独的变量(例如self ):

function doop() {
    var old = $(this).siblings('.old').html();
    var new = $(this).siblings('.new').val();

    var self = this;

    $.ajax({
        url: 'doop.php',
        type: 'POST',
        data: 'before=' + old + '&after=' + new,
        success: function(resp) {
            if(resp == 1) {
                $(self).siblings('.old').html(new);
            }
        }
    });

    return false;
}

The success function will retain the value of self when invoked, and should behave as you expected. success函数在调用时将保留self的值,并且应该按预期运行。

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

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