答案 0 :(得分:0)
使用ELEMENT.cloneNode(true)
克隆元素,true
也克隆子节点:
var selectionCounter = 0
function cloneSelect() {
var select = document.getElementById("statement")
var clone = select.cloneNode(true)
var name = select.getAttribute("name") + selectionCounter++
clone.id = name
clone.setAttribute("name", name)
document.getElementById("selectContainer").appendChild(clone)
}

<div id="selectContainer">
<select id="statement" name="statement">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
<button onclick="cloneSelect()">Add another Select</button>
&#13;
此处有关node.cloneNode(deep)
的更多信息:https://developer.mozilla.org/en/docs/Web/API/Node/cloneNode
答案 1 :(得分:-1)
我认为它会对你有帮助 -
<script>
function addOption(){
var sel = document.getElementById("statement");
var option = document.createElement("option");
option.innerHTML = "name";
option.value = "1";
sel.appendChild(option);
}
</script>
答案 2 :(得分:-1)
试试这个。它可能会帮助你。
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="container"></div>
<button type="button" id="add" onclick="addOption()">Add a statement</button>
<?php
$que = mysql_query("SELECT name FROM Statements") or die(mysql_error());
$script = "<script>function addOption(){";
$script .= "var container = document.getElementById('container');";
$script .= "var select = document.createElement('select');";
while($data = mysql_fetch_array($que)){
$script .= "var option = document.createElement('option');";
$script .= "option.innerHTML = '$data[0]';";
$script .= "select.appendChild(option);";
}
$script .= "container.appendChild(select);";
$script .= "}</script>";
echo $script;
?>
</body>
</html>