如何将使用javascript动态创建的HTML表格发送到另一个页面(servlet,jsp等)?
我的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function createTable(numberOfRows) {
for ( var i = 1; i < numberOfRows; i++) {
var tr = document.createElement("tr");
var tableData = ['hello','world','hi','bye'];
for ( var j = 0; j < tableData.length; j++) {
var td = document.createElement("td");
var data = document.createTextNode(tableData[j]);
td.appendChild(data);
tr.appendChild(td);
}
document.getElementById("table1").appendChild(tr);
}
}
function sendTableToAnotherPage(){
// **what should be the code here,to fetch the table created using above function
//and send it to servlet or jsp page via AJAX.**
}
</script>
</head>
<body>
<table id="table1">
</table>
<button type="button" onclick="createTable(5)">Create Table</button>
<input type="button" value="Save" onclick="sendTableToAnotherPage()">
</body>
</html>
我在从servlet / jsp页面获取表数据时遇到了困难。 任何帮助,将不胜感激。
答案 0 :(得分:0)
如果表和数据相同,则可以将table.innerHTML传递给另一页 例如:
<script type="text/javascript">
function createTable(numberOfRows) {
for ( var i = 1; i < numberOfRows; i++) {
var tr = document.createElement("tr");
var tableData = ['hello','world','hi','bye'];
for ( var j = 0; j < tableData.length; j++) {
var td = document.createElement("td");
var data = document.createTextNode(tableData[j]);
td.appendChild(data);
tr.appendChild(td);
}
document.getElementById("table1").appendChild(tr);
}
}
function sendTableToAnotherPage(){
document.getElementById("table2").innerHTML = document.getElementById("table1").innerHTML;
}
</script>
<table id="table2" border="2">
</table>
<table id="table1">
</table>
<button type="button" onclick="createTable(5)">Create Table</button>
<input type="button" value="Save" onclick="sendTableToAnotherPage()">