javascript - 添加/删除按钮不适用于重复的表单字段 - jquery

标签 javascript jquery html clone

我使用了以下答案中的代码:jquery clone form fields and increment id在我的表单中创建了一个可克隆的区域,除了“添加另一个”和“删除行”按钮外,一切都运行良好。

只有原始可克隆区域的添加/删除按钮起作用,因此下一个克隆区域的添加/删除按钮不会执行任何操作,这意味着您使用的是第一行,这很令人困惑。

我真的看不出来是怎么回事。任何帮助将不胜感激!

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();
});

最佳答案

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

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

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

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

通过这样做,事件处理程序被添加到 document所以任何时候一个事件都会冒出它,它起源于 button.clone元素,它将触发该功能。因此,当页面加载后添加按钮时,它们仍会触发点击事件,即使它们在页面最初加载时不可用也是如此。

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

关于javascript - 添加/删除按钮不适用于重复的表单字段 - jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19367546/

相关文章:

javascript - 如何获取字符串的第一个字符?

javascript - 由于 div 设置为不显示,排序表交替颜色未显示

php - HTML5 Builder - 更改页面和组合框中的奇怪字符

javascript - 检查数组中的所有值是否相同

javascript - 如何检查字符串是否包含javascript中的WORD?

jquery - 从嵌入式YouTube html5播放器获取<video>元素

javascript - 添加新项目的功能

html - Google 字体加载说明

javascript - 用JS随机改变几个div的背景图片

javascript - 如何使用 jQuery 修改 POST headers onclick?