【发布时间】:2016-01-30 23:47:32
【问题描述】:
我在单击按钮时克隆 DIV 元素,我能够更改我正在克隆的 DIV 元素的 ID 值。但是是否可以更改内部元素的 id。
在下面的代码中,我在克隆时更改了#selection 的ID,我需要动态更改ID #select。
<div id="selections">
<div class="input-group" id="selection">
<span class="input-group-addon">
<i class="icon wb-menu" aria-hidden="true"></i>
</span>
<select class="show-tick" data-plugin="select2" id="select">
<option>True</option>
<option>False</option>
</select>
</div>
</div>
<button class="btn btn-primary" type="button" style="margin-left: 30px;">
Add new selection
</button>
下面的JS
$(function() {
//on click
$("body").on("click", ".btn-primary", function() {
alert($(".input-group").length)
var
//get length of selections
length = $(".input-group").length,
//create new id
newId = "selection-" + length++,
//clone first element with new id
clone = $("#selection").clone().attr("id", newId);
//append clone on the end
$("#selections").append(clone);
});
});
【问题讨论】:
标签: javascript jquery twitter-bootstrap clone