如何在表格单元格中插入输入字段

How insert an input field in a table cell?

本文关键字:插入 输入 字段 单元格 表格      更新时间:2023-09-26

对不起的新手问题:我想创建一个表的输入字段,在那里可以添加新的,如果他们需要。但我不能弄清楚如何添加另一个输入字段在另一个输入字段已经存在的单元格内。

我的代码是:
var par=obj.parentNode;
while(par.nodeName.toLowerCase()!='tr')
{ par=par.parentNode; }
// this gives me the index of the row.. works fine.
cell1=document.getElementById('avz_tabelle').rows[par.rowIndex].cells;
// this puts the content of the cells into the array "cell1"
var feld = cell1[1].createElement("input"); 
feld.setAttribute("name","avz_keywords" + avz_array + "[]"); 
feld.setAttribute("onblur","");
feld.setAttribute("type","text"); 
feld.setAttribute("size","30"); 
// Now I create a input element
// And now comes the problem:
cell1[1].appendChild(feld); // --> doesn't work

有谁给我一个建议吗?我有了不用桌子工作的想法,从理论上讲,这符合我的方式。但这并不令人满意:/

如果您查看调试控制台,您应该看到类似

的内容。
TypeError: Object #<HTMLDivElement> has no method 'createElement'

使用document.createElement()

代替从cell1[1]创建元素
 var feld = document.createElement("input");

您应该使用document.createElement("input");而不是cell1[1].createElement("input");

一如既往(这里的另一个答案也说明了这一点),JavaScript控制台应该是寻找问题提示的第一个地方!

如果你想要添加值的每个表格单元格都有一个单独的ID,那不是更容易吗?包含行索引和列索引…然后直接选择