如何使用JQuery删除动态行?

时间:2018-01-03 16:46:45

标签: javascript jquery

我正在使用现在正在运行的按钮添加行,但我有另一个要删除的按钮,但无效。 这是我在JS中的代码:

jQuery(document).ready(function () {

    var collectionCount = 0;

    jQuery('#add-another-collection').click(function (e) {
        e.preventDefault();

        var collectionList = jQuery('#collection-fields-list');

        var newWidget = collectionList.attr('data-prototype');

        newWidget = newWidget.replace(/__name__/g, collectionCount);
        collectionCount++;


        var newTr = jQuery('<tr></tr>').html(newWidget);
        newTr.appendTo(collectionList);

    });

    $('.remove-collection').click(function (e) {

        console.log('fdsffdsfds');
        e.preventDefault();
        $(this).parent().remove();

        return false;
    });
})

我放了一个console.log来检查我是否正在那里但不是。

这是按钮:

<button type="button" class="btn btn-danger remove-collection">Delete</button>

1 个答案:

答案 0 :(得分:0)

如果您的按钮已动态添加到DOM,则必须使用以下单击处理程序:

$(document).on('click', '.remove-collection', function () {

});