【发布时间】:2013-02-04 04:12:16
【问题描述】:
我需要在 $.ajax 调用 PHP 数据库查询的成功函数中插入几行数据。
这是我到目前为止的想法(它不起作用,我在 $.each() 行的控制台“意外字符串”中收到语法错误):
// This is the checkbox element in the row that was clicked on
//$('#tblMovieList tbody tr').find('td input.chkDeleteMovie[value="' + movieID + '"]').parent()
console.log('row html: ' + $('#tblMovieList tbody tr').find('td input.chkDeleteMovie[value="' + movieID + '"]').parent().parent().next().html());
//actorID, actorFirstName, actorLastName
$('#tblMovieList tbody tr').find('td input.chkDeleteMovie[value="' + movieID + '"]').parent().parent().after('<tr id="actorsForMovie' + movieID + '">' +
'<td colspan="2"></td>' +
'<td colspan="8">' +
'<table id="tblActorsForMovie">' +
$.each(data,function(index,element) {
'<tr>' +
'<td>' +
'actorID: ' + element['actorID'] + ', name: ' + element['actorFirstName'] + ' ' + element['actorLastName'] +
'</td>' +
'</tr>' +
});
'</table>' +
'</td>' +
'</tr>';
);
我需要做的是插入一行,其中包含来自 php 脚本的 json 数据在该行有一个复选框,该复选框的值设置为特定的电影 ID。
console.log 行正确定位了 我需要将新行附加到的行之后的行。 (这有意义吗?)
因此,具有 after() 函数的选择器是正确的。我只是不确定在遍历 json 数据时如何准确地插入新行。
我需要怎么做?
附加信息 要插入的行的最终结构应如下所示:
</tr><!-- closing tag of existing row that the new row is being appended to -->
<tr id="actorsForMovie">
<td colspan="10"> <!-- There are 10 columns in the "parent" table -->
<table>
<tr>
<td>
[actor data]
</td>
</tr>
<tr>
<td>
[actor data]
</td>
</tr>
... etc for additional rows/actors
</table>
</td>
</tr>
<tr> <!-- Opening tag of the existing row that **was** immediately after the row being appended to -->
我创建了a jsFiddle 供任何人帮助我解决这个问题。无论我尝试了什么,插入的表 1) 永远不会被包裹在 and 标记中以使其与“父”表一起使用,并且 2) 插入的表 always 只会放入表格其余部分中第一个单元格的区域。
【问题讨论】:
-
有没有人对如何进行调整以使表格正确插入行有任何建议?
-
注意。自从我的原始帖子和 chead 的原始答案以来,我添加了很多澄清信息和我为此设置的 jsFiddle 的链接。我真的很感激一些帮助!在此先感谢...
-
我在这里添加了很多信息。希望这足以让有人尽快帮助我。如果需要更多信息,请告诉我!
标签: jquery html-table insert row