ajax aftersuccess,Ajax jquery success scope

问题

I have this ajax call to a doop.php.

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); line isn\'t doing what it\'s supposed to do.

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. I\'ve created a new post

回答1:

First of all new is a reserved word. 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:

var that = this;

$.ajax({

// ...

success: function(resp) {

if(resp == 1) {

$(that).siblings('.old').html($new);

}

}

})

This is called a closure.

回答2:

You should use the context setting as in 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.

回答3:

this is bound to the object to which the executing function was applied. That could be some AJAX response object, or the global object (window), or something else (depending on the implementation of $.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? 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?

You do indeed need a way to capture the value of this before defining the success function. Creating a closure is the way to do this. You need to define a separate variable (e.g. 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.

来源:https://stackoverflow.com/questions/1570146/ajax-jquery-success-scope

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值