使用jQuery动态添加/删除HTML元素

Kri*_*s-I 3 jquery

我在这里发现了这个很棒的代码Demo.我只需要做一些改变.我想要

  • 一行一行删除当前行
  • 删除前的确认

你可以帮帮我吗 ?

谢谢,

更新1:我想要这个 替代文字http://imagik.fr/thumb/275405.jpeg

Rei*_*gel 5

演示

HTML

<form id="myForm">
    <div style="margin-bottom:4px;" class="clonedInput">
        <input type="button" class="btnDel" value="Delete" disabled="disabled" />
        <input type="text" name="input1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
    </div>
</form>?
Run Code Online (Sandbox Code Playgroud)

jQuery的

$(document).ready(function() {

    var inputs = 1; 

    $('#btnAdd').click(function() {
        $('.btnDel:disabled').removeAttr('disabled');
        var c = $('.clonedInput:first').clone(true);
            c.children(':text').attr('name','input'+ (++inputs) );
        $('.clonedInput:last').after(c);
    });

    $('.btnDel').click(function() {
        if (confirm('continue delete?')) {
            --inputs;
            $(this).closest('.clonedInput').remove();
            $('.btnDel').attr('disabled',($('.clonedInput').length  < 2));
        }
    });


});
Run Code Online (Sandbox Code Playgroud)

资源