【发布时间】:2015-12-27 09:09:54
【问题描述】:
每当单击按钮时,我的 HTML 中都需要重复一组输入。我目前有一种方法可以让我将 HTML 插入 DOM,但恐怕这不是一个非常可维护的解决方案。
目前,我有一组输入定义为-
<div class="input-row">
<input name="location-0-bol-0-name" id="location-0-bol-0-name" class="form-control">
<input name="location-0-bol-0-description" id="location-0-bol-0-description" class="form-control">
<input name="location-0-bol-0-weight" id="location-0-bol-0-weight" class="form-control">
<input name="location-0-bol-0-type" id="location-0-bol-0-type" class="form-control">
</div>
单击按钮时,我想复制元素和嵌套在其中的所有输入。但是,我需要通过将第二个数值加一来更新所有输入的名称和 ID 属性。例如,下一组输入如下-
<div class="input-row">
<input name="location-0-bol-1-name" id="location-0-bol-1-name" class="form-control">
<input name="location-0-bol-1-description" id="location-1-bol-0-description" class="form-control">
<input name="location-0-bol-1-weight" id="location-0-bol-1-weight" class="form-control">
<input name="location-0-bol-1-type" id="location-0-bol-1-type" class="form-control">
</div>
我想我可以使用 JQuery 提供的 clone() 函数,但我不确定如何迭代所有元素并正确更新属性。有人可以提供一些指导吗?
谢谢!
【问题讨论】:
标签: javascript jquery html regex dom