在ajax回调jquery中使用$(this)
我正在做一个jQuery.post
到一个php文件,该文件返回给我一个值。
问题是:为什么$(this)
dosent在回调函数中起作用? 通过使用$(this)
传递要显示的任何警报,返回我的null
$(".class").live("focusout", function(){ jQuery.post("phpfile.php", { someValue: someValue }, function(data) { // why the $(this) dosent work in the callback ? } ) });
在这种情况下, this
不再是同一个对象。 之前保存参考并稍后使用:
$(".class").live("focusout", function(){ var $this = $(this); jQuery.post("phpfile.php", { someValue: someValue }, function(data) { // 'this' inside this scope refers to xhr object (wrapped in jQuery object) var x = $this; } ) });
$(".class").live("focusout", function(){ var this = $(this); jQuery.post("phpfile.php",{ someValue: someValue },function(data){ // Now use this instead of $(this), like this.hide() or whatever. }) });
你的例子中的$(this)是指我想的$ .post。