Mob*_*baz 1 javascript jquery clone copy
我有一个div包含一些动态生成的下拉列表.
我想克隆它并将其添加到原始下面.原始代码是:
<div id="subjectselectiondiv" style="width:inherit;">
<h4>Select the subjects that you are able to tutor</h4>
<div id='choices'>
<script type="text/javascript">
var doc = document.getElementById("choices");
var subj_div = document.createElement('div');
subj_div.setAttribute('class', 'selectSearchBar');
subj_div.innerHTML = '<select id="level'+counter+'" name="level'+counter+'" class="selectSearchBar"><?php echo "<option>Level</option>"; while($row = mysqli_fetch_array($result_level, MYSQLI_ASSOC)){echo "<option value=".$row['id'].">".$row['level']."</option>";}?></select><div id="subject'+counter+'" style="display:inline;">sBar2</div><div id="topic'+counter+'" style="display:inline;">sbar3</div>';
doc.appendChild(subj_div);
</script>
<a href='#' class="more" onclick="Repeat()">Add another subject</a>
</div>
Run Code Online (Sandbox Code Playgroud)
我认为函数Repeat()可以用于:
function Repeat(){
counter++;
$('#choices').clone().appendTo('#subjectselectdiv');
event.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用 - 我错过了什么吗?
谢谢
名称不匹配:
<div id="subjectselectiondiv" style="width:inherit;">
^^^^^^
$('#choices').clone().appendTo('#subjectselectdiv');
^^^
Run Code Online (Sandbox Code Playgroud)
您尝试附加的内容不存在.最有可能的是,如果你打扰检查你的javascript控制台,你会看到关于不存在的元素的警告/错误.