【问题标题】:Jquery only appending to first class elementJquery 仅附加到第一类元素
【发布时间】: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


【解决方案1】:

基于append method documentation,您可以通过以下功能传递函数:

返回 HTML 字符串、DOM 元素、文本节点或 jQuery 对象的函数,以插入到匹配元素集中每个元素的末尾。接收集合中元素的索引位置和元素的旧 HTML 值作为参数。在函数中,this 指的是集合中的当前元素。

虽然以下代码与该规则不一致:

$('<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>')

    })
  )

您不会在传递的函数中返回任何结果。

【讨论】:

    【解决方案2】:

    您正在 append 语句中运行第二个循环 - 这意味着附加选项的循环在创建 &lt;select&gt; 之前运行。由于尚未创建 select 元素,$(".new-presentation-dropdown") 与 DOM 中的任何内容都不匹配。移动在所有.append() 语句之外附加选项的循环应该可以解决这个问题:

    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')
                        )
                )
        );
    
        // 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>')
        })
    });
    

    但是$(".new-presentation-dropdown") 将匹配每个创建的&lt;select&gt;,因此会产生奇怪的结果。我建议您将$('&lt;select&gt;') 分配给一个变量并将选项附加到该变量,然后再将其附加到表中,如下所示:(未经测试,但应该可以工作)

    data.forEach(function(player) {
        console.log("players");
    
        var $newPresentationDopdown = $('<select>').attr('class', 'new-presentation-dropdown');
    
        // loop through each presentation in database and add to dropdown
        presentationData.forEach(function (presentation) {
            console.log(presentation.name);
            $newPresentationDopdown.append('<option>' + presentation.name + '</option>')
        });
    
        $(".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($newPresentationDopdown)
                )
        );
    });
    

    【讨论】:

    • 啊,谢谢您的解释,现在一切都说得通了,并且正在按预期工作。非常感谢!
    • 为了避免一些开销,你可以在第一个循环之外创建选择,这样你只迭代一次(因为它看起来每个玩家的presentationData都是相同的?)然后为每个玩家附加它播放器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2011-03-13
    • 2021-12-08
    • 2021-01-20
    • 2019-04-03
    • 2018-03-06
    相关资源
    最近更新 更多