我的应用的这个页面是一个仪表板,由div
组成的'面板',就像这样
<div class='panel' id='usersPanel' data-request='users'>
<h1>Users</h1>
<p class='panel-contents'>Will be filled with AJAX results</p>
</div>
在页面加载时,它们通过AJAX填充
$(document).ready(function() {
$('.panel').each(function(index, value) {
$.get('/panel', {'content': $(this).attr('data-request')}, function(data) {
$(this).find('.panel-contents').html(data);
});
});
});
请求已正常发送。回复没问题。但我似乎无法将其写入.panel-contents
。我错过了什么吗?
答案 0 :(得分:3)
我相信问题在于你的成功功能。执行此操作时,等同于尝试不同的尝试:
$(document).ready(function() {
$('.panel').each(function(index, value) {
var panel = this;
$.get('/panel', {'content': $(this).attr('data-request')}, function(data) {
$(panel).find('.panel-contents').html(data);
});
});
});
答案 1 :(得分:2)
你不会在this
成功监听器中获得$.get
- 事先缓存this
,如下所示:
$(document).ready(function() {
$('.panel').each(function(index, value) {
var $this = $(this);
$.get('/panel', {'content': $(this).attr('data-request')}, function(data) {
$this.find('.panel-contents').html(data);
});
});
});