Javascript 添加/删除按钮不处理重复的表单字段-jquery

Javascript 添加/删除按钮不处理重复的表单字段-jquery,javascript,jquery,html,clone,Javascript,Jquery,Html,Clone,我使用了下面答案中的代码:创建了一个可克隆的表单区域,除了“添加另一个”和“删除行”按钮外,所有操作都正常 只有原始的可克隆区域的“添加/删除”按钮起作用,因此下一个克隆区域的“添加/删除”按钮不起任何作用,这意味着您必须使用第一行的按钮,这很容易混淆 我真的看不出发生了什么事。任何帮助都将不胜感激 HTML: <div id="previous-jobs"> <div class="panel-group" id="accordion"> <div 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>

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

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

将其添加到任何$(document).ready()函数之外:

$(document).on('click', 'button.clone', function(){
    ...
});
通过这样做,事件处理程序将被添加到
文档中
,因此,每当一个源自
按钮.clone
元素的事件出现时,它都会触发该函数。因此,当加载页面后添加按钮时,即使在页面最初加载时按钮不可用,它们仍会触发单击事件


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

还要告诉他为什么要使用该按钮。很抱歉,我没有早点回复,我没有收到电子邮件通知!非常感谢!!!这就把它修好了。现在我只需要从第一个可克隆区域中删除“删除行”按钮,这样他们就不能完全删除它。不管怎样,这都是一个很大的帮助,非常感谢!!
$(document).on('click', 'button.clone', function(){
    ...
});