18910140161

JavaScript-如何获取HTML克隆表单值-堆栈溢出

顺晟科技

2022-10-18 13:52:47

221

当用户克隆行时,

我试图从我的表单中获取值。当我克隆这些行时,ID是否仍然相同?如果是这样,如何使新行具有唯一的ID?

jQuery(function($) {
  var $button = $('#add-row'),
    $row = $('.period-row').clone();

  $button.click(function() {
    $row.clone().insertBefore($button);
  });
});
<代码><;script SRC=“ https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js ”>;<;/script>;<;H3 CLASS=“ HEADING ”>;请选择预约时间<;/H3>;<;DIV类=“表单包装器”>;<;表单action=“#” ID=“ date-form ” name=“ date-form ” method=“ post ”>;<;DIV类=“ period-row ”>;<;label for=“ start ”>;from:<;/label>;<;input type=“ datetime-local ” name=“ start[]” ID=“ start ”>;<;label for=“ end ”>;to:<;/label>;<;input type=“ datetime-local ” name=“ end[]” ID=“ end ”>;<;/DIV>;<;input type=“ button ” ID=“ add-row ” name=“ add-row ” value=“添加期间”/>;<;button type=“ Submit ”>;提交<;/button>;<;/窗体>;<;/DIV>;


顺晟科技:

我不明白你到底想要什么,要以正确的方式做到这一点,你必须在输入上使用类,而不是ID,因为ID是唯一的。但我希望这段代码对你有帮助。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3 class="heading">Please select appointment period</h3>
<div class="form-wrapper">
  <form action="#" id="date-form" name="date-form" method="POST">
    <div class="period-row">
      <label for="start">From:</label>
      <input type="datetime-local" name="start[]" id="start">
      <label for="end">To:</label>
      <input type="datetime-local" name="end[]" id="end">
    </div>
    <input type="button" id="add-row" name="add-row" value="add period" />
    <button type="submit">submit</button>
  </form>
</div>
<代码><;script SRC=“ https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js ”>;<;/script>;<;H3 CLASS=“ HEADING ”>;请选择预约时间<;/H3>;<;DIV类=“表单包装器”>;<;表单action=“#” ID=“ date-form ” name=“ date-form ” method=“ post ”>;<;DIV类=“ period-row ”>;<;label for=“ start ”>;from:<;/label>;<;input type=“ datetime-local ” name=“ start[]” ID=“ start ”>;<;label for=“ end ”>;to:<;/label>;<;input type=“ datetime-local ” name=“ end[]” ID=“ end ”>;<;/DIV>;<;input type=“ button ” ID=“ add-row ” name=“ add-row ” value=“添加期间”/>;<;button type=“ Submit ”>;提交<;/button>;<;/窗体>;<;/DIV>;

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航