如何克隆一个表单并不止一次更改其子项的 id?

如何解决如何克隆一个表单并不止一次更改其子项的 id?

我一直在尝试克隆作为表单一部分的部分,我已成功完成该部分。但是我需要处理数据,所以我需要更改元素子元素的 ID(输入,选择 ecc)。我一直在网上找了一段时间,但没有什么对我有用,也是因为我想更频繁地克隆元素,而且 ID 应该每次都改变。 你能帮我解决这个问题吗?会很棒:

HTML:

<div class="input-form fcf-form-group">

  <div class="form only">
    <h3 class="fcf-h3">Room</h3>
    <div class="form-section">

      <div class="title-group">
        <label for="room" class="fcf-label">Room</label>
        <select name="room" id="room" required>
          <option value="double">Double</option>
          <option value="single">Single</option>
        </select>
      </div>
    </div>
  </div>

  <div class="form neighbour">

    <div class="title-group small">
      <label for="adults" class="fcf-label">Adults</label>
      <input type="number" id="adults" name="adults" min="1" max="5">
    </div>

    <div class="title-group small">
      <label for="child" class="fcf-label">Children</label>
      <input type="number" id="child" name="child" min="1" max="5">
    </div>

    <div class="title-group small">
      <label for="age" class="fcf-label">Age of the Child</label>
      <select name="age" id="age" required>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>
    </div>
  </div>
</div>
</div>

查询

$('#but_add').click(function () {

  // Create clone of <div class='input-form'>
  var newel = $('.input-form:last').clone();

  // Add after last <div class='input-form'>
  $(newel).insertAfter('.input-form:last');
});
})

解决方法

您可以首先通过选择器克隆表单(就像您所做的那样),然后通过 multiple selector 搜索每个表单元素(输入和选择)。存储它们后,您可以映射它们,然后通过 .attr 更新输入元素 id 以在每次执行此操作时添加索引(例如:房间,然后是房间 1,然后是房间,...)。需要明确的是,您可以将初始 HTML 中的每个 id 重命名为“0”(例如:room0)。

最后,您将拥有每个 id 递增的表单数据。

,

此代码将克隆原始表单,为克隆中的每个 idfor 属性附加一个唯一编号,并将克隆添加到容器底部:

function cloneAndChangeIds(wrapper,text) {

  // clone the wrapper
  const clone = wrapper.cloneNode(true);

  // add text to the `for` attribute of every label
  const labels = Array.from(clone.querySelectorAll('label'));
  labels.forEach(label => {
    label.htmlFor += text;
  });

  // add text to the `id` attribute of every select and input 
  const items = Array.from(clone.querySelectorAll('select,input'));
  items.forEach(select => {
    select.id += text
  });

  return clone;
}

let iClone = 0;

// get container and original wrapper
const container = document.getElementById('container');
const original = document.querySelector('.input-form');
const cloneBtn = document.getElementById('clone-button');

function appendAClone() {
  // clone original and modify IDs
  let clone = cloneAndChangeIds(original,'-1');

  // add clone at end of container div
  container.appendChild(clone);
}

cloneBtn.onclick = appendAClone;
<button id="clone-button">Make A Clone</button>
<p>The original:</p>
<hr>
<div id="container">
  <div class="input-form fcf-form-group">

    <div class="form only">
      <h3 class="fcf-h3">Room</h3>
      <div class="form-section">

        <div class="title-group">
          <label for="room" class="fcf-label">Room</label>
          <select name="room" id="room" required>
            <option value="double">Double</option>
            <option value="single">Single</option>
          </select>
        </div>
      </div>
    </div>

    <div class="form neighbour">

      <div class="title-group small">
        <label for="adults" class="fcf-label">Adults</label>
        <input type="number" id="adults" name="adults" min="1" max="5">
      </div>

      <div class="title-group small">
        <label for="child" class="fcf-label">Children</label>
        <input type="number" id="child" name="child" min="1" max="5">
      </div>

      <div class="title-group small">
        <label for="age" class="fcf-label">Age of the Child</label>
        <select name="age" id="age" required>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
      </div>
    </div>
    <hr>
  </div>
</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?