使用ajax向表中添加新行
add a new row to a table with ajax
我有点问题。事实上,我意识到每次添加一行时,都会考虑到其他添加。也就是说,如果我加一行,好吧,它被插入点击的下面。但如果我想在新的下面添加,它实际上添加了两行等等。有什么帮助吗?这是我的代码
<script>
$(".insertRow").live('click', function () {
var vref = '@Url.Action("RecordEntryRow", "Layout")';
var obj = this;
$.ajax({
url: vref,
cache: false,
success: function (html) {
var u = $(obj).parents(".record:first.")[0];
$(u).after(html);
}
});
return false;
});
<tr class ="record">
@using (Html.BeginCollectionItem("Records")) {
<td> @Html.DropDownListFor(model => model.Type, new List<SelectListItem>{
new SelectListItem() {Text = "titre en gras", Value="Titre_Gras"},
new SelectListItem() {Text = "titre normal", Value="Titre_Normal"},
new SelectListItem() {Text = "titre minime", Value="Titre_Minime"},
new SelectListItem() {Text = "titre italique", Value="Titre_Italique"},
new SelectListItem() {Text = "item", Value="Item"},
new SelectListItem() {Text = "Separateur", Value="Separateur"},
}) </td>
<td> @Html.EditorFor(model => model.Contenu) </td>
<td> <a href="#" onclick="$(this).closest('tr').remove();"><img src="~/Content/images/red-delete-button.jpg" width="25" height="25" /></a></td>
<td> <a href="#" class="insertRow" ><img src="~/Content/images/add_button.png" width="20" height="20" /></a> </td>
}
我对您编辑的问题进行了测试。一切如预期:
<!doctype html>
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var i = 0;
$(document).off('click', '#td').on('click', 'td', function(e) {
$(this).parent().after('<tr><td>Append ' + i + '</td></tr>');
i++;
e.preventDefault();
});
});
</script>
</head>
<body>
<table>
<tr>
<td id="td" style="border: 1px solid red;">
Test
</td>
</tr>
</table>
</body>
</html>
相关文章:
- 如何在脚本上添加新行
- 在不刷新整个网格的情况下,向Asp.net网格视图添加新行
- dhtmlx添加新行并启动单元格编辑器
- 如何在Google Geochart图表的工具提示文本中添加新行
- 如何在javascript中每次添加新行时都增加ordre值
- 两个表添加新行按钮-冲突
- 在Spring.message属性中添加新行时出错
- 使用Javascript在文本区域中添加新行
- 在java脚本的第一行之前添加新行
- sails.js更新或添加新行
- 使用ajax向表中添加新行
- 使用一些默认值在剑道网格中添加新行
- 添加新行后无法“刷新”灯丝组表锯响应表
- JQuery 数据表添加新行,如果该行已经存在,请将其删除并为该 ID 插入新行
- 续集 - 向表添加新行并关联表
- 如何在 jQuery 函数 text() 中添加新行
- 在表格顶部添加新行
- 添加新行动态引导日期时间选择器不起作用
- 如何在不影响 Jquery 数据表中其他行的情况下删除选定的现有数据并添加新行
- 分机.js确认方法添加新行字符