【发布时间】:2016-11-03 18:24:11
【问题描述】:
对于第一次迭代它工作正常,表按预期创建。但是在第二次迭代时,下拉菜单是空的。循环都正常工作,所以我假设它必须与我如何附加选择选项有关。感谢任何帮助。
Javascript
function loadPlayers() {
//first ajax call to retrieve players
$.ajax({
url: '/players',
type: 'GET',
contentType: "application/json",
success: function(data) {
//second ajax call to retrieve presentations
$.ajax({
url: '/presentations',
type: 'GET',
contentType: "application/json",
success: function(presentationData) {
// loop through each player and append player/presentation to table
data.forEach(function(player) {
console.log("players")
$(".player-table").append(
$('<tr>').append(
$('<td>').attr('class', 'player-td').append(player.name),
$('<td>').attr('class', 'presentation-td').append(player.presentation),
$('<td>').attr('class', 'new-presentation-td').append(
$('<select>').attr('class', 'new-presentation-dropdown').append(
// loop through each presentation in database and add to dropdown
presentationData.forEach(function(presentation) {
console.log(presentation.name);
$(".new-presentation-dropdown").append('<option>' + presentation.name + '</option>')
})
)
)
)
)
})
}
})
}
})
}
HTML
<table class="player-table">
<tr class="player-name-tr">
<th>Player Name</th>
<th>Current Presentation</th>
<th>New Presentation</th>
</tr>
</table>
【问题讨论】:
-
请看这个链接。你的问题是 async stackoverflow.com/questions/23283276/… 。您应该在 .complete() 中编写第二个 ajax
-
尝试引用您创建的选择并将选项附加到引用中。选择器 $(".new-presentation-dropdown") 每次迭代都有 1 个元素,可能会破坏这里的东西。
-
@Memme 这是我的想法,但我想不出另一种方法来引用有效的选择...
-
下面的 Anser 完全暴露了我的意思 :-)
标签: javascript jquery