对 table 中的最后一行进行 AJAX 加载调用

Making AJAX load call to last row in a table

我有一个 table 看起来像

<table id="MyTable">
 <thead>/*Some th elements*/</thead>
  <tbody>
    <tr></tr>
    <tr></tr>
    <tr></tr>
  </tbody>
</table>

我正在尝试使用 AJAX Load() 方法将 tr 附加到最后一行。

这是我的代码,

$('#MyTable tbody tr:last').parent().load("/Index/LoadMoreData", dataObject);

这是创建嵌套的 tr ,这种类型的结构

<table id="MyTable">
 <thead>/*Some th elements*/</thead>
  <tbody>
    <tr></tr>
    <tr></tr>
    <tr>
      <tr></tr> // want this tr like other trs
    </tr>
  </tbody>
</table>

这不是我所期望的。请让我知道适当的选择器以将 tr 加载到正确的结构。 TIA

load 方法只替换您选择的元素的内容,它不会附加到父元素。您需要使用 get 方法并编写自己的函数来追加该行。

$.get('/Index/LoadMoreData', dataObject, function(data){ 
  $('#MyTable tbody').append(data);
});

试试这个:-

$('#MyTable tbody tr:last').after($('<tr/>').load("/Index/LoadMoreData", dataObject));

如果数据已经预先包装好 tr 标签,那么你可以使用这个:-

$('#MyTable tbody tr:last').after($('<tr/>').load("/Index/LoadMoreData", dataObject).contents().unwrap());