Before you clone the row, you need to disable Select2 on the select element by calling its destroy
method:
destroy
Reverts changes to DOM done by Select2. Any selection done via Select2 will be preserved.
See http://ivaynberg.github.io/select2/index.html
After you clone the row and insert its clone in the DOM, you need to enable select2 on both select elements (the original one and the new cloned one).
Here's a JSFiddle that shows how it can be done: http://jsfiddle.net/ZzgTG/
Fiddle's code
HTML
<div id="contents">
<select id="sel" data-placeholder="-Select education level-">
<option></option>
<option value="a">High School</option>
<option value="b">Bachelor</option>
<option value="c">Master's</option>
<option value="c">Doctorate</option>
</select>
</div>
<br>
<button id="add">add a dropdown</button>
CSS
#contents div.select2-container {
margin: 10px;
display: block;
max-width: 60%;
}
jQuery
$(document).ready(function () {
$("#contents").children("select").select2();
$("#add").click(function () {
$("#contents")
.children("select")
// call destroy to revert the changes made by Select2
.select2("destroy")
.end()
.append(
// clone the row and insert it in the DOM
$("#contents")
.children("select")
.first()
.clone()
);
// enable Select2 on the select elements
$("#contents").children("select").select2();
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…