在ajax之后对表进行排序



我想用这里的代码对一个表进行排序。

但是表是在Ajax调用之后加载的,在JS中我无法获得表,尽管我可以获得表类。

下面是一个简化的代码。

我省略了排序代码,因为问题就在这之前(请参阅临时检查代码(。

有人能告诉我我做错了什么吗?

$(document).on('click', '[id^="sortTable-"]', function() {
var Exploted = this.id.split("-");
var n = Exploted[1];
var WhichTable = Exploted[2];
//Temporary checking code
var XX = $("#" + WhichTable).attr('class');
alert(XX);
//OK, I get the table class (TheTableClass), meaning that the table is being detected, even though it was loaded after the initial page load (with Ajax)
var theTable = $('#' + WhichTable);
var rows = theTable.rows;
if (theTable.lenght) {
alert("ok, table detected, and it has " + rows + " rows");
} else {
alert("Table NOT detected! Has rows? " + rows);
}
//in the previous if statement, I get "Table NOT detected! Has rows? undefined"
//End of Temporary checking code
//here the sorting code from
//https://www.w3schools.com/howto/howto_js_sort_table.asp
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="TheTableClass" id="MyTableId">
<tbody>
<tr>
<th id="sortTable-0-MyTableId">Name</th>
<th id="sortTable-1-MyTableId">Surname</th>
<th id="sortTable-2-MyTableId">Age</th>
</tr>
<tr>
<td>John</td>
<td>Tom</td>
<td>32</td>
</tr>
<tr>
<td>Alex</td>
<td>Jerry</td>
<td>28</td>
</tr>
</tbody>
</table>

此处length拼写错误theTable.lenght。这应该是theTable.length。除此之外,我看不到任何其他问题。

最新更新