![](/img/trans.png)
[英]how to add and remove select box with options and text box with label and style using javascript
[英]How to add and remove a select box with options?
我正在一个用户页面上显示选择选项,例如城市名称abc和城市名称bcd。 我想从管理页面到用户页面动态添加和删除城市。 我是Java脚本和HTML的新手。
在用户页面上:
<script src="add.js"></script>
<form>
<select id="newCity">
<option>city1</option>
<option>city2</option>
<option>city3</option>
<option>city4</option>
</select>
</form>
管理员页面:
<script src="add.js"></script>
<form>
<select id="newCity">
<option>city1</option>
<option>city2</option>
<option>city3</option>
<option>city4</option>
</select>
</form>
<button type="button" onclick="myFunction()">Insert city</button>
add.js文件:
function myFunction() {
var x = document.getElementById("newCity");
var option = document.createElement("option");
option.text = "new city";
x.add(option);
}
您需要在两个页面都可以从中检索数据的地方
我将使用数组
您需要在用户页面加载时调用one(),以便选项值立即更新
我添加了另一个按钮
<button type="button" onclick="one()">Insert</button>
然后在javascript中,我添加了用于保存值的数组
myarray = ["city1","city2","city3","city4","yo"];
MyFunction的()
...
var x = document.getElementById("newCity");
var option = document.createElement("option");
value="new";
option.text = value;
myarray.push(value);
x.add(option);
...
一()
...
var x = document.getElementById("newCity");
x.innerHTML= "";
for (i=0;i<myarray.length;i++){
var option = document.createElement("option");
value=myarray[i];
option.text = value;
x.add(option);
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.