[英]How to add and remove a select box with options?
I am working on a user page that displays select option like eg city name abc, and city name bcd. 我正在一个用户页面上显示选择选项,例如城市名称abc和城市名称bcd。 I want to dynamically add and remove cities from admin page to user page.
我想从管理页面到用户页面动态添加和删除城市。 I am new to java script and HTML.
我是Java脚本和HTML的新手。
On user page: 在用户页面上:
<script src="add.js"></script>
<form>
<select id="newCity">
<option>city1</option>
<option>city2</option>
<option>city3</option>
<option>city4</option>
</select>
</form>
Admin page: 管理员页面:
<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 file: add.js文件:
function myFunction() {
var x = document.getElementById("newCity");
var option = document.createElement("option");
option.text = "new city";
x.add(option);
}
You need to have a place from where both pages can retrieve data 您需要在两个页面都可以从中检索数据的地方
I will use array 我将使用数组
You need to call one() on user page load so that option values update immediately 您需要在用户页面加载时调用one(),以便选项值立即更新
I've added another button 我添加了另一个按钮
<button type="button" onclick="one()">Insert</button>
And then in javascript I've added array for holding values 然后在javascript中,我添加了用于保存值的数组
myarray = ["city1","city2","city3","city4","yo"];
myfunction() MyFunction的()
...
var x = document.getElementById("newCity");
var option = document.createElement("option");
value="new";
option.text = value;
myarray.push(value);
x.add(option);
...
one() 一()
...
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.