这是 javascript 代码,我想在每次创建新元素时更改名称属性。每次添加新表行时,我都会创建新的文本框。但是当我尝试从 servlet 访问这些元素的值时,它会显示空值。我到底做错了什么?任何帮助,将不胜感激。提前谢谢!!
<html>
<head>
<script>
function addRow()
{
var table = document.getElementById('table');
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "text";
element1.name ="item"+rowCount;
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
element2.name ="amount"+rowCount;
cell2.appendChild(element2);
var cell3 = row.insertCell(2);
cell3.innerHTML = ' <input type="button" value="Edit" onclick="editRow(this)"/> <input type="button" value="Delete" onclick="deleteRow(this)"/>';
cell3.setAttribute("style", "display:none;");
var cell4 = row.insertCell(3);
cell4.innerHTML = '<input type="button" value="Save" onClick="saveRow(this)">';
document.listform.hfield.value=rowCount;
}
function submitList()
{
document.listform.submit();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create your list</title>
</head>
<body>
<form name="listform" method="post" action="Billingservlet">
<div class="wrap">
<center>
<div id="display">
<table id='table' border="0">
<tr id='id'>
<th>Item Name</th>
<th>No. of units</th>
</tr>
</table>
<input type="button" value="Add another item" onclick="addRow()">
<input type="button" value="Submit List" onclick="submitList()">
<input type="hidden" name="hfield">
我的 Servlet 代码是
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out= response.getWriter();
int i=1;
String val1=request.getParameter("item2");
out.print(val1);
我使用 chrome 检查了哪些数据将发送到 servlet:
hfield:2
Response Headersview parsed
HTTP/1.1 200 OK
server: Apache-Coyote/1.1
Content-Length: 4
Date: Fri, 13 Sep 2013 02:21:29 GMT