Javascript 动态添加下拉列表
我可以动态添加如下文本框:Javascript 动态添加下拉列表,javascript,php,html,sql,dynamically-generated,Javascript,Php,Html,Sql,Dynamically Generated,我可以动态添加如下文本框: <html> <body> <div id='item'></div> <input name='add' type='button' id='add' value='Add Item'/> </body> </html> <script type="text/javascript"> var item = document.getElementById('ite
<html>
<body>
<div id='item'></div>
<input name='add' type='button' id='add' value='Add Item'/>
</body>
</html>
<script type="text/javascript">
var item = document.getElementById('item');
var stopper=0;
document.getElementById('add').onclick = function () {
stopper=stopper+1;
var input = document.createElement('input'),
div = document.createElement('div');
input.type = "text";
input.setAttribute("name", "item[]");
input.setAttribute("class", "item");
div.appendChild(input);
//...
item.appendChild(div);
};
</script>
var item=document.getElementById('item');
var=0;
document.getElementById('add')。onclick=function(){
塞子=塞子+1;
var input=document.createElement('input'),
div=document.createElement('div');
input.type=“text”;
input.setAttribute(“名称”、“项[]”);
input.setAttribute(“类”、“项”);
div.appendChild(输入);
//...
项目.儿童(分部);
};
我该怎么做?我希望动态生成一个下拉列表(
),而不是文本框。下拉菜单的选项将来自我的SQL数据库。将createElement('input')
替换为createElement('select')
,然后以相同的方式创建其他选项
元素:var opt1=document.createElement('option')
并设置属性(进入服务器的值和用户通常看到的文本)在每个元素上。然后将每个选项
对象附加到选择
对象,并像现在处理输入
对象一样将选择
对象附加到DOM中
下面是一个使用动物数组并将第一个字母作为值发送到服务器的示例:
var items = ['aardvark', 'bear', 'cat', 'donkey'];
var values = ['a', 'b', 'c', 'd'];
var sel = document.createElement('select');
sel.setAttribute('name', 'item[]');
for (var i = 0; i < 4; i++) {
var opt = document.createElement('option');
opt.setAttribute('text', items[i]);
opt.setAttribute('value', values[i]);
sel.appendChild(opt);
}
// ... from here on, set any other attributes, like classes
// Then and add the select object to the DOM:
item.appendChild(sel);
var items=['aardvark'、'bear'、'cat'、'驴'];
var值=['a','b','c','d'];
var sel=document.createElement('select');
sel.setAttribute('name','item[]);
对于(变量i=0;i<4;i++){
var opt=document.createElement('option');
opt.setAttribute('text',items[i]);
opt.setAttribute('value',value[i]);
选择附加儿童(opt);
}
//…从现在开始,设置任何其他属性,如类
//然后将select对象添加到DOM中:
项目.儿童(sel);
您好,请尝试此代码
<html>
<body>
<div id='item'></div>
<input name='add' type='button' id='add' value='Add Item'/>
</body>
</html>
<script type="text/javascript">
var item = document.getElementById('item');
var stopper=0;
document.getElementById('add').onclick = function () {
stopper=stopper+1;
var select = document.createElement('select'),
div = document.createElement('div');
select.setAttribute("name", "item[]");
select.setAttribute("class", "item");
//this is just an example. You need to replace this code with ajax that is fetching
//from the Database.. and please use jquery it makes your work easier.
var count = getRandomInt(1, 5);
for(var a=1; a<=count; a++) {
var option = document.createElement('option');
var t = randon_val();
//end
option.setAttribute("value",t);
option.appendChild(document.createTextNode(t));
select.appendChild(option);
}
div.appendChild(select);
item.appendChild(div);
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randon_val()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
</script>
var item=document.getElementById('item');
var=0;
document.getElementById('add')。onclick=function(){
塞子=塞子+1;
var select=document.createElement('select'),
div=document.createElement('div');
选择.setAttribute(“名称”、“项[]”);
select.setAttribute(“类”、“项”);
//这只是一个示例,您需要将此代码替换为正在获取的ajax
//请使用jquery,它使您的工作更容易。
变量计数=getRandomInt(1,5);
对于(var a=1;a而言,此链接将非常有用。
你也可以这样试试
函数addItemToList(){
//为下拉列表创建标签
var label=document.createElement(“标签”);
label.innerHTML=“选择一项:”
//下拉值
var valueraray=[“V1”、“V2”、“V3”、“V4”];
var textArray=[“TEXT1”、“TEXT2”、“TEXT3”、“TEXT4”];
//创建下拉列表
var select=document.createElement(“select”);
select.name=“dropdownName”;
select.id=“dropdownId”
//下拉项
对于(变量i=0;i
检查此答案欢迎使用指向解决方案的链接,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户就会知道它是什么以及为什么存在,然后引用您链接到的页面的最相关部分,以防目标页面不可用。虽然此链接可以回答问题,但最好是使用inc请在此处删除答案的重要部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。-答案还添加了