简体   繁体   English

jQuery仅附加到一流元素

[英]Jquery only appending to first class element

for the first iteration of this it works fine, the table is created as intended. 对于此方法的第一次迭代,它工作正常,该表已按预期创建。 However upon the second iteration, the drop down menu is empty. 但是,在第二次迭代时,下拉菜单为空。 The loops are all working correctly so I'm assuming it has to be something to do with how I am appending the option to select. 循环都正常工作,因此我假设这与我附加选择选项有关。 Appreciate any help. 感谢任何帮助。

Javascript Java脚本

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 的HTML

  <table class="player-table">
    <tr class="player-name-tr">
      <th>Player Name</th>
      <th>Current Presentation</th>
      <th>New Presentation</th>
    </tr>
  </table>

You're running the 2nd loop inside the append statement - it means the loop appending the options is run before the <select> is created. 您正在append语句中运行第二个循环-这意味着附加选项的循环在创建<select>之前运行。 Since the select element has not yet been created $(".new-presentation-dropdown") matches nothing in the DOM. 由于尚未创建select元素,因此$(".new-presentation-dropdown")在DOM中不匹配任何内容。 Moving the loop appending the options outside of all the .append() statements should fix this issue: 将循环添加选项到所有.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>')
    })
});

However $(".new-presentation-dropdown") will match every <select> created so that'll give strange results. 但是$(".new-presentation-dropdown")将与创建的每个<select>匹配,从而给出奇怪的结果。 I'd suggest you assign the $('<select>') to a variable and append the options to the variable, before appending it to the table, like this: (Untested but should work) 我建议您将$('<select>')分配给变量,然后将选项附加到变量,然后再将其附加到表中,如下所示:(未经测试,但应该可以)

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

Based on append method documentation you can pass function by below feature: 根据附加方法文档,您可以通过以下功能传递功能:

A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements. 该函数返回HTML字符串,DOM元素,文本节点或jQuery对象,以插入到匹配元素集中的每个元素的末尾。 Receives the index position of the element in the set and the old HTML value of the element as arguments. 接收元素在集合中的索引位置以及该元素的旧HTML值作为参数。 Within the function, this refers to the current element in the set. 在函数内,这是指集合中的当前元素。

Whiles below code is inconsistent by that rule: 以下代码与该规则不一致:

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

    })
  )

You don't return any result in your passed function. 您不会在传递的函数中返回任何结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM