动态创建的下拉多个追加不能工作
dynamically created dropdown multiple appends don't work
我有一个动态创建的<select>
。当我单击一个按钮时,它应该被添加到div的末尾。然而,当我多次单击时,它没有被添加多次,而我期望它被添加多次。
完整代码如下:
<html>
<head>
<title>New Recipe</title>
<script type="text/javascript">
function showfield(name, hiddenId, inputName){
if(name == 'New'){
document.getElementById(hiddenId).innerHTML='New: <input type="text" name="other" />';
}
else {
document.getElementById(hiddenId).innerHTML='';
}
}
</script>
</head>
<body>
<script type="text/javascript">
var selectBox = document.createElement("select");
var option = document.createElement("option");
option.text = "Chocolade: Zwart";
option.value = "1-1";
selectBox.add(option);
var option = document.createElement("option");
option.text = "Zuivel: Boter(80%)";
option.value = "2-2";
selectBox.add(option);
var option = document.createElement("option");
option.text = "Zuivel: Room(40%)";
option.value = "2-3";
selectBox.add(option);
var option = document.createElement("option");
option.text = "Smaakmakers: Koffie-extract";
option.value = "3-4";
selectBox.add(option);
function addNewIngredient(divId){
var amountPart = "Amount(in kg or l): <input type='"text'"/>";
console.log("clicked");
//document.getElementById(divId).appendChild(selectBox + amountPart + '<br/>');
document.getElementById(divId).appendChild(selectBox);
}
</script>
<form method="post" action="/new/recipe">
category: <select name="category" onchange="showfield(this.options[this.selectedIndex].value, 'newCategory')">
<option value="NONE">Please select ....</option>
<option value="New">New</option>
</select>
<div id="newCategory"></div><br/>
<div id="ingredientList"></div>
<input type="button" onclick="addNewIngredient('ingredientList')" value="Add Ingredient"/>
<input type="submit" value="Submit">
</form>
</body>
</html>
JSFiddle: https://jsfiddle.net/w42m6voj/
PS: HTML由Bottle(python框架)生成。我没有硬编码选项中的值,它们是从数据库中获取的。
您只创建了一个selectbox实例:
var selectBox = document.createElement("select");
然后重用它。因此,它被从DOM的旧位置删除并插入到另一个位置。
下面是它的工作原理:
function createNewSelectBox() {
var selectBox = document.createElement("select");
var option = document.createElement("option");
option.text = "Chocolade: Zwart";
option.value = "1-1";
selectBox.add(option);
var option = document.createElement("option");
option.text = "Zuivel: Boter(80%)";
option.value = "2-2";
selectBox.add(option);
var option = document.createElement("option");
option.text = "Zuivel: Room(40%)";
option.value = "2-3";
selectBox.add(option);
var option = document.createElement("option");
option.text = "Smaakmakers: Koffie-extract";
option.value = "3-4";
selectBox.add(option);
return selectBox;
}
function addNewIngredient(divId){
var amountPart = "Amount(in kg or l): <input type='"text'"/>";
console.log("clicked");
//document.getElementById(divId).appendChild(selectBox + amountPart + '<br/>');
document.getElementById(divId).appendChild(createNewSelectBox());
}
相关文章:
- 不能从angular2中的子组件指定父组件中的数组
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- 转义符不能与innerHTML一起使用
- JSON.parse没有'不能使用Javascript
- JS可以在Chrome中工作,但不能在Firefox中工作
- 砌体不能填补小缺口
- jQuery追加双引号;不能正常工作
- 不能将元素追加到表单数据
- jQuery换行获胜'按下回车键时不能追加
- 不能用JQuery追加一个选择框
- 鼠标输入和追加不能正常工作
- 追加时不能访问jQuery
- 为什么弹出窗口不能'工作时追加数据到主体表
- 动态创建的下拉多个追加不能工作
- 不能向新包装的元素追加jquery元素
- Ajax调用和追加,不能选择追加的内容或做任何事情
- 不能追加到SVG,即使在使用命名空间之后
- 不能使用 jquery parseXML 追加 XML 节点,但可以追加字体
- jQuery不能访问追加元素