[英]Javascript function to append table with cells containing input field and value
I created a working function that appends a table with cells/rows and then populates those cells with data from an array. 我创建了一个工作函数,它附加一个带有单元格/行的表,然后使用数组中的数据填充这些单元格。 The problem is now I need to change it so that the generated cells contain an input
field where the value=
is the array data. 问题是现在我需要更改它,以便生成的单元格包含一个input
字段,其中value=
是数组数据。
Here is an example of the closest I have been able to come: http://jsfiddle.net/JEAkX/3/ 这是我能够来到的最近的一个例子: http : //jsfiddle.net/JEAkX/3/
at one point I tried changing to cell.innerHTML = "<input type='text' size='1'>"+ subset[i++] +"</input>"
but it just put the data outside of the input text area. 有一次我尝试改为cell.innerHTML = "<input type='text' size='1'>"+ subset[i++] +"</input>"
但它只是将数据放在输入文本区域之外。
subset[i++]
creates the correct output but I cant seem to get it inside a field value. subset[i++]
创建了正确的输出,但我似乎无法将其置于字段值中。
Here is the function before I edited it for an input field: 这是我为输入字段编辑它之前的功能:
function appendTable(id)
{
var tbody = document.getElementById(id).getElementsByTagName("tbody")[0];
var i = 0;
for (var r = 0; r < 4; r++) {
var row = tbody.insertRow(r);
for (var c = 0; c < 4; c++) {
var cell = row.insertCell(c);
cell.appendChild(document.createTextNode(subset[i++]));
}
}
}
Instead of doing: 而不是做:
cell.innerHTML = "<input type='text' size='1'>"+ subset[i++] +"</input>"
Try to do this: 尝试这样做:
cell.innerHTML = "<input type='text' size='1' value='" + subset[i++] + "'/>"
Using the value property of the input, this way it should work. 使用输入的value属性,这样它应该工作。
You were on the right track, you need to add the text to the input value like: 您在正确的轨道上,您需要将文本添加到输入值,如:
cell.innerHTML = "<input type='text' size='1' value='" + subset[i++] + "' />";
and your subset needs to be populated with some values. 并且您的子集需要填充一些值。
Use this: 用这个:
value=''+subset[i++]
instead of 代替
value='subset[i++]'
So your new function should read like : 所以你的新功能应该是这样的:
function appendTable(id)
{
var tbody = document.getElementById(id).getElementsByTagName("tbody")[0];
var i = 0;
for (var r = 0; r < 4; r++) {
var row = tbody.insertRow(r);
for (var c = 0; c < 4; c++) {
var cell = row.insertCell(c);
cell.innerHTML = "<input type='text' size='1' value=''+subset[i++] />"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.