Javascript 添加列表框
我在我的页面中使用了两个按钮。添加和删除 如果按Add,则应添加一行文本框和一个列表框。 “删除”用于撤消上一个操作 我使用的代码是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
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等。