从表中删除行时将删除表标头

发布于 2025-02-12 04:40:49 字数 3627 浏览 2 评论 0原文

当我从桌子上删除行时,也正在删除表标头,如何解决此问题?

$('#clubs tbody').on('click', '.deleteBtn', function() {
   $(this).closest('tr').remove();  
});

按钮标记

<td>
  <button class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn" 
                     [email protected]("Delete", "Resources")></button>
</td>

我的按钮具有一类.delete的类,因此,当我单击它时,将我的行与我的table header一起删除。

我的表格和 <代码>俱乐部 <代码>表 clubsstoby的ID。

<div class="table-responsive">
  <table class="table table-striped my-4 " id="clubs">
    <thead>
      <tr>
        <th>@DbResHtml.T("#", "Resources")</th>
        <th>@DbResHtml.T("Клуб", "Resources")</th>
        <th>@DbResHtml.T("Лига", "Resources")</th>
        <th></th>
      </tr>
    </thead>
    <tbody id="clubsTBody">
@foreach (var club in Model.Player.PlayerClubs)
  {
      <tr>
        <td>@Html.DisplayFor(x => count)</td>
        <td>@Html.DisplayFor(x => club.Club.Name)</td>
        <td>@Html.DisplayFor(x => club.Club.League.LeagueType)</td>
        <td>
          <button class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn"
                             [email protected]("Delete", "Resources")></button>
        </td>
      </tr>
  count++;
  }
    </tbody>
  </table>
</div>

也​​将动态排入表中。

$(document).ready(function() {
  $('#select2-3').change(function() {
    var cc  = $('#select2-3').val();
    var ids = [];
    for (let i = 0; i < cc.length;i++) {
      ids.push(cc[i]);
    }
    $.ajax({
      type    : "POST",
      url     : "@Url.Action("GetClubsById","Player")",
      data    : {"ids": ids},
      success : function(data) {
        console.log(data);
        $('#clubs tr').remove();
        var counter = 1;
        for (let i = 0; i < data.length; i++) {
          $("#clubsTBody").append("<tr><td>" + counter + "</td>"
            + "<td>" + data[i].name + "</td>"
            + "<td>" + data[i].league.leagueType + "</td>"
            + "<td>" + '<button  class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn"  [email protected]("Delete", "Resources")></button>' + "</td>" 
            + "</tr >");
          counter++;
        }
      },
      error: function(req, status, error) {
          console.log(msg);
      }
    });
    
    $('.deleteBtn').on('click', function() {
      $(this).closest('tr').remove();

      var value = $(this).closest('tr').children('td:eq(1)').text();
      $(`#select2-3 option:selected:contains("${value}")`).prop("selected", false).parent().trigger("change");
    });
  })
    
// /.../

})

问题在这里,当我从SELECT列表中删除所选项目时,没有此代码,一切都可以正常工作,但是选择的项目并未取消选择。

$(`#select2-3 option:selected:contains("${value}")`)
  .prop("selected", false)
  .parent()
  .trigger("change");

When I am deleting row from my table, also the table header is being deleted, how I can fix this?

$('#clubs tbody').on('click', '.deleteBtn', function() {
   $(this).closest('tr').remove();  
});

Button tag

<td>
  <button class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn" 
                     [email protected]("Delete", "Resources")></button>
</td>

My button have a class of .delete so when I click it, my row is deleted together with my table header.

My table have and id of clubs while table body have and id of clubsTBody.

Table

<div class="table-responsive">
  <table class="table table-striped my-4 " id="clubs">
    <thead>
      <tr>
        <th>@DbResHtml.T("#", "Resources")</th>
        <th>@DbResHtml.T("Клуб", "Resources")</th>
        <th>@DbResHtml.T("Лига", "Resources")</th>
        <th></th>
      </tr>
    </thead>
    <tbody id="clubsTBody">
@foreach (var club in Model.Player.PlayerClubs)
  {
      <tr>
        <td>@Html.DisplayFor(x => count)</td>
        <td>@Html.DisplayFor(x => club.Club.Name)</td>
        <td>@Html.DisplayFor(x => club.Club.League.LeagueType)</td>
        <td>
          <button class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn"
                             [email protected]("Delete", "Resources")></button>
        </td>
      </tr>
  count++;
  }
    </tbody>
  </table>
</div>

Also I am adding dynamically rows into my table.

$(document).ready(function() {
  $('#select2-3').change(function() {
    var cc  = $('#select2-3').val();
    var ids = [];
    for (let i = 0; i < cc.length;i++) {
      ids.push(cc[i]);
    }
    $.ajax({
      type    : "POST",
      url     : "@Url.Action("GetClubsById","Player")",
      data    : {"ids": ids},
      success : function(data) {
        console.log(data);
        $('#clubs tr').remove();
        var counter = 1;
        for (let i = 0; i < data.length; i++) {
          $("#clubsTBody").append("<tr><td>" + counter + "</td>"
            + "<td>" + data[i].name + "</td>"
            + "<td>" + data[i].league.leagueType + "</td>"
            + "<td>" + '<button  class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn"  [email protected]("Delete", "Resources")></button>' + "</td>" 
            + "</tr >");
          counter++;
        }
      },
      error: function(req, status, error) {
          console.log(msg);
      }
    });
    
    $('.deleteBtn').on('click', function() {
      $(this).closest('tr').remove();

      var value = $(this).closest('tr').children('td:eq(1)').text();
      $(`#select2-3 option:selected:contains("${value}")`).prop("selected", false).parent().trigger("change");
    });
  })
    
// /.../

})

The problem is here, when I am removing selected item from select list, without this code, everything is working perfectly but selected items doesn't get deselected.

$(`#select2-3 option:selected:contains("${value}")`)
  .prop("selected", false)
  .parent()
  .trigger("change");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2025-02-19 04:40:49
$(document).ready(function() {
        $('#select2-3').change(function() {
            var cc = $('#select2-3').val();
            var ids = [];
            for (let i = 0; i < cc.length;i++){
                ids.push(cc[i]);
            }

            $.ajax({
                type: "POST",
                url: "@Url.Action("GetClubsById","Player")",
                data: {"ids": ids},
                success: function(data) {
                    console.log(data);
                    $('#clubsTBody tr').remove();
                    var counter = 1;
                    for (let i = 0; i < data.length; i++) {
                    $("#clubsTBody").append("<tr><td>" + counter + "</td>"
                    + "<td>" + data[i].name + "</td>"
                    + "<td>" + data[i].league.leagueType + "</td>"
                    + "<td>" + '<button  class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn"  [email protected]("Delete", "Resources")></button>' + "</td>" 
                    + "</tr >");
                        counter++;
                    }
                },
                error: function(req, status, error) {
                    console.log(msg);
                }
            });

          $('.deleteBtn').on('click', function() {
            $(this).closest('tr').remove();

            var value = $(this).closest('tr').children('td:eq(1)').text();
            $(`#select2-3 option:selected:contains("${value}")`).prop("selected", false).parent().trigger("change");
        });
        });

您要写这篇文章: $('#clubstbody tr')。remove(); 而不是 $('#clubs tr')。remove();
您正在删除Ajax响应中的所有TR,而不是只有身体TR

$(document).ready(function() {
        $('#select2-3').change(function() {
            var cc = $('#select2-3').val();
            var ids = [];
            for (let i = 0; i < cc.length;i++){
                ids.push(cc[i]);
            }

            $.ajax({
                type: "POST",
                url: "@Url.Action("GetClubsById","Player")",
                data: {"ids": ids},
                success: function(data) {
                    console.log(data);
                    $('#clubsTBody tr').remove();
                    var counter = 1;
                    for (let i = 0; i < data.length; i++) {
                    $("#clubsTBody").append("<tr><td>" + counter + "</td>"
                    + "<td>" + data[i].name + "</td>"
                    + "<td>" + data[i].league.leagueType + "</td>"
                    + "<td>" + '<button  class="mb-1 btn bg-danger fas fa-trash-alt deleteBtn"  [email protected]("Delete", "Resources")></button>' + "</td>" 
                    + "</tr >");
                        counter++;
                    }
                },
                error: function(req, status, error) {
                    console.log(msg);
                }
            });

          $('.deleteBtn').on('click', function() {
            $(this).closest('tr').remove();

            var value = $(this).closest('tr').children('td:eq(1)').text();
            $(`#select2-3 option:selected:contains("${value}")`).prop("selected", false).parent().trigger("change");
        });
        });

You nee to write this: $('#clubsTBody tr').remove(); instead of $('#clubs tr').remove();
You are removing all the TR in Ajax Response instead of Only Body TRs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文