使用 Javascript 将单元格动态添加到 JSP 页面。需要从单元中收集数据并将数据发送到数据库。使用 Spring MVC、hibernate、spring form 标签
任何解释这一点的文章都非常感谢!
任何建议都会有所帮助!
javascript:
var i=0;
function addRow()
{
i++;
var tbl = document.getElementById('div1');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
var row = tbl.insertRow(lastRow);
var firstCell = row.insertCell(0);
var el = document.createElement('input');
el.type = 'text';
el.name = 'hostName' + i;
el.id = 'hostName' + i;
el.size = 30;
el.maxlength = 200;
firstCell.appendChild(el);
var secondCell = row.insertCell(1);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'directory' + i;
el2.id = 'directory' + i;
el2.size = 30;
el2.maxlength = 200;
secondCell.appendChild(el2);
var thirdCell = row.insertCell(2);
var el3 = document.createElement('input');
el3.type = 'text';
el3.name = 'userName' + i;
el3.id = 'userName' + i;
el3.size = 20;
el3.maxlength = 20;
thirdCell.appendChild(el3);
var fourthCell = row.insertCell(3);
var el4 = document.createElement('input');
el4.type = 'text';
el4.name = 'password' + i;
el4.id = 'password' + i;
el4.size = 20;
el4.maxlength = 20;
fourthCell.appendChild(el4);
}
function removeRowFromTable()
{
var tbl = document.getElementById('div1');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
</script>`
动态创建的单元格:
<tr><td colspan="4">
<table id="div1" style="display:none">
<tr><td>Host Name</td><td>Directory</td><td>User Name</td><td>Password</td></tr>
<tr><td><input type="text" value="${events.hostName}" size="30" maxlength="200"/></td>
<td><input type="text" value="${events.directory}" size="30" maxlength="200"/></td>
<td><input type="text" value="${events.userName}" size="20" maxlength="20"/></td>
<td><input type="text" value="${events.password}" size="20" maxlength="20"/></td></tr>
</table>
</td></tr>
<tr id="i1" style="display:none"><td><input type="button" onclick="addRow()" value="+"/>
<input type="button" onclick="removeRowFromTable();" value="-" />
</td></tr>