在表jquery中插入一行

时间:2015-05-14 01:24:10

标签: javascript jquery html

我的桌子最初有一排 enter image description here 并且用户可以在此行中输入一些信息,单击添加按钮后,该行将信息存储为新行并显示在表中 喜欢: enter image description here 这是我写的代码:

 var insert_html='<tr><td>'+attr.title + '</td><td>' + attr.attribute + '</td><td>' + attr.value + '</td> <td>literal_eval</td> <td class="delete_button">  <button class="delete_attr">delete</button> </td></tr>';
    $(insert_html).insertBefore($(this).closest('tr'));

但如果我想连续添加另一行,它将失败 这是详细信息:

https://jsfiddle.net/m8a0v9y8/

它适用于jsfiddle,但是当我在我的大项目中使用它时,每次单击添加时,页面都会自动刷新。

所以我在chrome开发人员工具中逐步完成,在第一次显示正常与jsfiddle相同之后,可以在输入行之前添加新的行插入 enter image description here enter image description here enter image description here 然后页面自动刷新

1 个答案:

答案 0 :(得分:3)

看起来你的insert_html变量缺少打开表行标记。它应该是:

var insert_html='<tr><td>'+attr.title + '</td><td>' + attr.attribute + '</td><td>' + attr.value + '</td> <td>literal_eval</td> <td class="delete_button">  <button class="delete_attr">delete</button> </td></tr>';

您的jsfiddle链接不正确。