javascript - jquery克隆表单字段并增加id

标签 javascript jquery

我有一个表单元素 block ,我想使用 jQuery 克隆方法克隆并增加它们的 ID。我尝试了很多示例,但其中很多仅克隆单个字段。

我的 block 的结构如下:

<div id="clonedInput1" class="clonedInput">
  <div>
    <div>
      <label for="txtCategory" class="">Learning category <span class="requiredField">*</span></label>
      <select class="" name="txtCategory[]" id="category1">
        <option value="">Please select</option>
      </select>
    </div>
   <div>
     <label for="txtSubCategory" class="">Sub-category <span class="requiredField">*</span></label>
     <select class="" name="txtSubCategory[]" id="subcategory1">
       <option value="">Please select category</option>
     </select>
   </div>
   <div>
     <label for="txtSubSubCategory">Sub-sub-category <span class="requiredField">*</span></label>
     <select name="txtSubSubCategory[]" id="subsubcategory1">
       <option value="">Please select sub-category</option>
     </select>
   </div>
</div>

显然元素排列得更好,但你明白了。

我想保留 id 结构,即类别 1、子类别 1 等,因为我使用这些结构根据父选择动态显示选择选项,因此如果可以拥有每个克隆 block (如类别 1/类别 2/类别 3 等),那就太好了.

最佳答案

HTML

<div id="clonedInput1" class="clonedInput">
    <div>
        <label for="txtCategory" class="">Learning category <span class="requiredField">*</span></label>
        <select class="" name="txtCategory[]" id="category1">
            <option value="">Please select</option>
        </select>
    </div>
    <div>
        <label for="txtSubCategory" class="">Sub-category <span class="requiredField">*</span></label>
        <select class="" name="txtSubCategory[]" id="subcategory1">
            <option value="">Please select category</option>
        </select>
    </div>
    <div>
        <label for="txtSubSubCategory">Sub-sub-category <span class="requiredField">*</span></label>
        <select name="txtSubSubCategory[]" id="subsubcategory1">
            <option value="">Please select sub-category</option>
        </select>
    </div>
    <div class="actions">
        <button class="clone">Clone</button> 
        <button class="remove">Remove</button>
    </div>
</div>

JavaScript - Jquery v1.7 及更早版本

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

$("button.clone").live("click", function(){
    $(this).parents(".clonedInput").clone()
        .appendTo("body")
        .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);
            }
    });
    cloneIndex++;
});

只有一个愚蠢的部分:) .attr("id", "clonedInput"+ $(".clonedInput").length) 但它有效;)

JAvascript - JQuery 最近(支持 .on())

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

function clone(){
    $(this).parents(".clonedInput").clone()
        .appendTo("body")
        .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);
            }
        })
        .on('click', 'button.clone', clone)
        .on('click', 'button.remove', remove);
    cloneIndex++;
}
function remove(){
    $(this).parents(".clonedInput").remove();
}
$("button.clone").on("click", clone);

$("button.remove").on("click", remove);

工作示例here

关于javascript - jquery克隆表单字段并增加id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8018132/

相关文章:

javascript - 当作为字符串传递时使用 '+' 作为实际的 + 运算符 - React

javascript - 使用 Ajax 和 PHP 的联系表单中缺少变量

jquery - 选择带有悬停的 jquery 子项

javascript - 在 Ace 编辑器中将我的值推到自动完成列表的顶部

javascript - angularJS中$http(req)中的return语句和defer.resolve有什么区别

angularjs - 附加到一个方法?代码理解

javascript - 无法从作为类型模块导入的js文件调用函数

jQuery 悬停菜单

javascript - 我如何在此代码中折叠 ="false"

javascript - jQuery 标签 "Undefined is Not a Function"