4

我使用了以下答案中的代码: jquery clone form fields and increment id to create an clone-able area of​​ my form,除了“添加另一个”和“删除行”按钮外,一切都运行良好。

只有原始可克隆区域的添加/删除按钮有效,因此下一个克隆区域的添加/删除按钮不会做任何事情,这意味着您使用了令人困惑的第一行。

我真的看不到发生了什么。任何帮助将不胜感激!

的HTML:

<div id="previous-jobs">
<div class="panel-group" id="accordion">
    <div id="clonedInput1" class="clonedInput panel panel-default">
        <div class="panel-heading clearfix">
            <h4 class="panel-title pull-left">
                <a class="heading-reference accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse1">
                    Entry #1
                </a>
            </h4>
            <div id="action-buttons" class="pull-right">
                <button type="button" class="btn btn-success clone">Add another</button>
                <button type="button" class="btn btn-danger remove">Delete Row</button>
            </div>
        </div>
        <div id="collapse1" class="input-panel panel-collapse collapse">
            <div class="panel-body">
                <div class="row">
                    <div class="form-group col-sm-6">
                        <label class="p-date-from-title input-title" for="p-date-from">Date From</label>
                        <input type="text" class="ci-df form-control" id="p-date-from1" name="p-date-from" value="[[+!fi.p-date-from]]" placeholder="Date from">
                    </div>
                    <div class="form-group col-sm-6">
                        <label class="p-date-to-title input-title"  for="p-date-to">Date To</label>
                        <input type="text" class="ci-dt form-control" id="p-date-to1" name="p-date-to" value="[[+!fi.p-date-to]]" placeholder="Date to">
                    </div>
                </div>
                <div class="form-group">
                    <label class="p-employer-title input-title"  for="p-employer">Employer</label>
                    <input type="text" class="ci-em form-control" id="p-employer1" name="p-employer" value="[[+!fi.p-employer]]" placeholder="Employer">
                </div>
                <div class="form-group">
                    <label class="p-position-title input-title"  for="p-position">Position</label>
                    <input type="text" class="ci-po form-control" id="p-position1" name="p-position" value="[[+!fi.p-position]]" placeholder="Position">
                </div>
                <div class="form-group">
                    <label class="p-salary-title input-title"  for="p-salary">Salary</label>
                    <input type="text" class="ci-sa form-control" id="p-salary1" name="p-salary" value="[[+!fi.p-salary]]" placeholder="Salary">
                </div>
                <div class="form-group">
                    <label class="p-full-part-time-title input-title"  for="p-full-part-time">Full/Part Time</label>
                    <select class="ci-fpt form-control" id="p-full-part-time1" name="p-full-part-time" value="[[+!fi.p-full-part-time]]">
                        <option value="Full Time">Full Time</option>
                        <option value="Part Time">Part Time</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="p-leaving-reason-title input-title"  for="p-leaving-reason">Reason for Leaving</label>
                    <input type="text" class="ci-rfl form-control" id="p-leaving-reason1" name="p-leaving-reason" value="[[+!fi.p-leaving-reason]]" placeholder="Reason for leaving">
                </div>
            </div>
        </div>
    </div>
</div>

jQuery:

var regex = /^(.*)(\d)+$/i;
var cloneIndex = $(".clonedInput").length;

$("button.clone").click(function(){
cloneIndex++;
$(this).parents(".clonedInput").clone()
    .appendTo("#accordion")
    .attr("id", "clonedInput" +  cloneIndex)
        .find("*").each(function() {
        var id = this.id || "";
        var match = id.match(regex) || [];
        if (match.length == 3) {
                this.id = match[1] + (cloneIndex);
                this.name = match[1] + (cloneIndex);
    }
    $(this).find('.heading-reference').attr("href", "#collapse" + cloneIndex).html('Entry #' + cloneIndex);
    });
});

$("button.remove").click(function(){
    $(this).parents(".clonedInput").remove();
});
4

1 回答 1

1

对于动态添加的元素,请使用.on() jQuery 文档 .on() (适用于 jQuery 1.7 及更高版本)

在您发布的示例中,他们使用.live()的是 jQuery 1.7 及以下版本。

在任何 $(document).ready() 函数之外添加:

$(document).on('click', 'button.clone', function(){
    ...
});

通过这样做,事件处理程序会被添加到事件处理程序中,document因此每当一个源自button.clone元素的事件冒泡到它时,它将触发该函数。因此,当页面加载后添加按钮时,它们仍然会触发点击事件,即使在页面最初加载时它们不可用。

如果您只使用$('button.clone').click(...)首次加载时页面上的按钮将触发点击事件。

于 2013-10-14T19:20:37.857 回答