2010-11-10 154 views
1

此代码确实获得了每个具有“profileName”类的元素的索引值。它也使用每个索引值作为url运行$ .get。但是,当我尝试使用每个“profilName”div的子元素“details”类将ajax响应放入每个元素时,它都会失败。这是可行的吗?

$('.profileName').each(function(index) { 
    var urlVal = $(".showProfile", this).attr('profile'); 
    $.get(urlVal, function(data) { 
     $(".details", this).html(data); 
    }, "html"); 
}); 

回答

5

这是因为this并不是指你想要的$.get()success回调里面是什么。您可以通过将其存储事前,这样解决它:

$('.profileName').each(function(index) { 
    var urlVal = $(".showProfile", this).attr('profile'), 
     details = $(".details", this); 
    $.get(urlVal, function(data) { 
    details.html(data); 
    }, "html"); 
}); 

$.get()使用$.ajax()下,和if a context option isn't specified, the ajax object itself is this in the callback

+0

尼克,非常感谢!你救了我的工作;) – 2010-11-10 19:16:29