jquery - 在 tr 之前添加行不起作用

标签 jquery html

我的问题与我现有的问题相关。请引用我的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/

相关文章:

jquery - 如何在更改后使用 jQuery 选择 CSS 类

html - 如何删除 h1 标签中文本周围的默认空格

html - 使用 Rails 获取单选按钮选择

javascript - 辅助功能 - 如何找到对比度问题?

html - 扩展 Font Awesome

javascript - HTML5 Canvas 从浏览器拖出到桌面

jQuery 平滑滚动 anchor 导航

javascript - 组合绑定(bind)事件而不丢失上下文

javascript - 自定义轻量级 JavaScript 库 : EnderJS and MicroJS

jquery - AJAX 调用 Select 元素中的多重选择