简体   繁体   English

在按钮单击时在另一个 div 内添加带有内容的 div

[英]Add div with content inside another div on button click

I have a simple dropdown and a checkbox as children of a div.我有一个简单的下拉列表和一个复选框作为 div 的子项。 I am trying to replicate this div when a user clicks on the "Add Another" button.当用户单击“添加另一个”按钮时,我试图复制此 div。

This is my div:这是我的 div:

<div id="dominantDiv">
    <div id="childDiv">
        <div>
            Dominant Item
            @Html.CheckBox("checkbox", true, new { @onClick = "toggleSelect()" })
            @Html.Hidden("hidCheck")
        </div>
        <div>
            @Html.DropDownList("Drop", (IEnumerable
                           <SelectListItem>)TempData["Drop"], new { @id = "enableDrop", required = true })
            @Html.Hidden("hidSelection")
            <button type="button" id="addDominantBtn" onclick="addDominant()">Add Another</button>
        </div>
    </div>
</div>

I've already tried using appendChild我已经尝试过使用 appendChild

function addDominant() {
        $("#dominantDiv").appendChild($("#childDiv"));
    }

to get the whole div and re-add it to its parent but I get the error获取整个 div 并将其重新添加到其父级,但出现错误

Uncaught TypeError: $(...).appendChild is not a function
    at addDominant
    at HTMLButtonElement.onclick

You mean append .你的意思是append

And also, you need clone to create a new child element.而且,您需要clone来创建一个新的子元素。

function addDominant() {
  $("#dominantDiv").append($("#childDiv").clone());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM