我的问题与我现有的问题相关。请引用我的fiddle
如果我单击“添加行”按钮,将添加 tr。我想在“添加行”按钮之前添加 tr。
我尝试了以下代码:
$tr.prependTo($(this).closest('tr'));
至
$tr.insertBefore($(this).closest('tr'));
但是没有用。我无法修复它。任何人都可以节省我的时间吗?
最佳答案
使用on()如live()已从较新的 jQuery 版本 中删除。还可以使用计数器类
,因为id
必须是唯一的
$(".AddRow").on('click',function () {
var index = $(this).closest('tr').index();
var $tr = $(this).closest('tr').clone(true);
var $input = $tr.find('input.startdatum');
var index = $tr.find('input.counter').val();
$tr.find('.AddRow').val('Delete') // set value to Delete
.toggleClass('AddRow delete') // toggle classes
.unbind('click'); // unbind the current events from button
var id = 'datepicker' + index;
index++;
$tr.find('input.counter').val(index);
$input.attr('id', id).data('index', index);
$tr.prependTo($(this).closest('table'));
setdatepicker();
});
// bind click event to delete entire row
$(document).on('click','.delete',function(){
$(this).closest('tr').remove();
});
<强> Live Demo
此外,如果您看到代码,那么如果您将 index
应用为 new,那么它是
然后你就可以看到差异。prepending
行而不是 appending
tr class
关于jquery - 在 tr 之前添加行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23003969/