1

我需要在同一个类的最后一个之后插入一个div包含 a的克隆。但是我怎样才能清空所有的表单域呢?我有以下代码:formdiv

<span id="add">Add</span>
<div id="0" class="addr">
    <input type="text" name="a">
    <input type="text" name="b">
    <input type="text" name="c">
</div>
<script>
    jQuery(document).ready(function($){
        $('#add').on('click',function() {
            var id = $(".addr:last").attr('id');
            $(".addr:last").clone().attr('id', ++id).insertAfter(".addr:last");
        });
    });
</script>
4

4 回答 4

1
var temp = $(".addr:last").clone()
temp = $(temp).(':input').val('');
$(temp).attr('id', ++id).insertAfter(".addr:last");
于 2014-03-18T12:30:25.300 回答
1

尝试使用$(".className").empty();

Remove all child nodes of the set of matched elements from the DOM
于 2014-03-18T12:32:58.397 回答
1

您可以使用以下内容来清理 DIV 中的字段

$('.addr').find('input:text').val('');
于 2014-03-18T12:37:16.810 回答
1

您可以使用该 div 的 html 来追加它,而不是克隆 div

$('#add').on('click', function () {
        var id = $(".addr:last").attr('id');
        var appendDiv = jQuery($(".addr:last")[0].outerHTML);
            appendDiv.attr('id', ++id).insertAfter(".addr:last");
    });
于 2014-03-18T12:39:05.087 回答