jquery 为什么不克隆div并将其复制到新div

pxq42qpu  于 10天前  发布在  jQuery
关注(0)|答案(1)|浏览(2)

我试图克隆一个div并将其附加到另一个div。但是为什么克隆在此代码中不起作用?

<form>
  <div id="newAutomaticItemDiv" class="col s10 hidden" hidden="hidden">
    <article class="row" id="invoiceLineItem">
      <label>Select GL Account</label>
      <select class="combobox padding-top-2x" name="GeneralLedgerAccount.LookupTypeId" data-val-lookup_required="GL Account field is required for Auto Generated invoice setup.">
        <option value=""></option>
        <option value="1">text 1</option>
        <option value="2">3</option>
      </select>

      <input type="text" />
    </article>
  </div>

  <div id="AutomaticInvoiceItemList">

  </div>
  <div class="row">
    <button type="button" onclick="addAutomaticInvoiceLineItem()">
      <span>Add GL</span>
    </button>
  </div>
</form>

我想从newAutomaticItemDiv克隆到AutomaticInvoiceItemList
查询:

function addAutomaticInvoiceLineItem() {
  debugger;
  var autoInvoiceIndex = 0;
  var autogeneratedInvoicesContainer = $("#AutomaticInvoiceItemList" + autoInvoiceIndex);
  var newAutomaticItemDiv = $("#newAutomaticItemDiv");

  // Clone the empty Add New row and add it to the Automatic Invoices container
  var cloneAutomaticDiv = newAutomaticItemDiv.clone();
  autogeneratedInvoicesContainer.append(cloneAutomaticDiv.children()[0]);

}

Js小提琴https://jsfiddle.net/k0prs7jw/

9avjhtql

9avjhtql1#

我觉得这个逻辑不太顺,目的是把newAutomaticItemDiv复制到AutomaticInvoiceItemList,但是你是这样写代码的。

var autoInvoiceIndex = 0;
var autogeneratedInvoicesContainer = $("#AutomaticInvoiceItemList" + autoInvoiceIndex);
// refer one which id is AutomaticInvoiceItemList0

但这并不位于您的html代码中。

<div id="AutomaticInvoiceItemList">
    
</div>

请更新提及的部分。

相关问题