gpt4 book ai didi

javascript - 将循环计数传递给 ajax 成功匿名函数

转载 作者:行者123 更新时间:2023-11-30 08:11:03 24 4
gpt4 key购买 nike

在下面的示例中,key 每次都等于 d1.periods 的长度。除了将 key 作为 post 变量传递给 data 以将 key 传递给 之外,他们还有更好的方法吗?成功 匿名函数?

$.ajax({
url: 'api/dates.json',
dataType: 'json',
success: function(d1){
$('#scan-interactive').removeClass('disabled');
for(var key in d1.periods){

var html = "";
html += '<tr id="'+d1.periods[key].id+'">';
html += '<td class="month">'+d1.periods[key].month+'</td>';
html += '<td class="week">'+d1.periods[key].week+'</td>';
html += '<td class="days">'+d1.periods[key].days+'</td>';
html += '<td class="dates">'+d1.periods[key].dates+'</td>';
html += '<td class="open"></td>';
html += '<td class="refunded"></td>';
html += '<td class="closed"></td>';
html += "</tr>";
$('#scan-interactive table tbody').append(html);

$.ajax({
type: 'POST',
url: 'api/count.json',
dataType: 'json',
data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
success: function(d2){
$('#'+d1.periods[key].id+" .open").html(d2.open);
$('#'+d1.periods[key].id+" .closed").html(d2.closed);
$('#'+d1.periods[key].id+" .returns").html(d2.returns);
}
});

}
}
});

最佳答案

将它包裹在一个闭包中:

(function(key){

$.ajax({
type: 'POST',
url: 'api/count.json',
dataType: 'json',
data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
success: function(d2){
$('#'+d1.periods[key].id+" .open").html(d2.open);
$('#'+d1.periods[key].id+" .closed").html(d2.closed);
$('#'+d1.periods[key].id+" .returns").html(d2.returns);
}
});

})(key);

关于javascript - 将循环计数传递给 ajax 成功匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com