jquery+ajaxc回调函数内使用$(this)并不能正确找到对象的解决办法

来源:互联网 发布:雄迈 监控软件密码 编辑:程序博客网 时间:2024/04/27 02:53
由于jquery的代码是Function作用域,而

success:function(strValue){}的作用域改变了,因此在该作用域下使用$(this)并不能找到当前对象。


比较简单的解决办法是在调用ajax之前保存一下当前的this变量。

比如:

$(function(){

$(".yourClass").each(function(){

varPrice =$(this).html();

var$this =$(this);


$.ajax({

async:false,

type:"POST",

url:"modifyPriceAction.action",

dataType:"html",

data:{price:Price},

success:function(strValue){ 

$($this).html(strValue);

}

})

})

})


0 0
原创粉丝点击