Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加列表框_Javascript_Html - Fatal编程技术网

Javascript 添加列表框

Javascript 添加列表框,javascript,html,Javascript,Html,我在我的页面中使用了两个按钮。添加和删除 如果按Add,则应添加一行文本框和一个列表框。 “删除”用于撤消上一个操作 我使用的代码是 var cellRight = row.insertCell(5); var el = document.createElement('input'); el.setAttribute('type', 'text'); el.setAttribute('name', 'txtRow'); el.setAttribute('size', '15'); ce

我在我的页面中使用了两个按钮。添加和删除 如果按Add,则应添加一行文本框和一个列表框。 “删除”用于撤消上一个操作

我使用的代码是

var cellRight = row.insertCell(5);

var el = document.createElement('input');

el.setAttribute('type', 'text');

el.setAttribute('name', 'txtRow');

el.setAttribute('size', '15');

cellRight.appendChild(el);
如果我只使用文本框,这会很好地工作。添加列表框的步骤

var cellRight = row.insertCell(6);

var el = document.createElement('input');

el.setAttribute('type', 'select');

el.setAttribute('size', '15');

el.setAttribute('method','onclick');

cellRight.appendChild(el);
我写了这段代码,但当我按下“添加”按钮时,只有文本框而不是列表框。 我的代码正确吗?

没有
。您需要创建一个
选择
元素并添加
选项
子元素

大概是这样的:

var el = document.createElement('select');
el.setAttribute('name', 'txtRow');
el.setAttribute('size', '15');

var label = document.createTextNode('item1label');
var opt = document.createElement('option'); 
opt.setAttribute('value', 'item1value');
opt.appendChild(label);
el.appendChild(opt);

cellRight.appendChild(el);

注意,干编码,ymmv。

另外,值得一提的是,如果您没有遇到JS库,使用JS库会更简单。例如,jQuery、Prototype、MooTools等。