我有多个文本框示例。 但它只能制作一个文本框。 我想用按钮添加表格制作多个表格。这在我的样本中
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function appendRow()
{
var d = document.getElementById('plus');
d.innerHTML += "<input type='text' name='nama'><br >";
}
</script>
</head>
<body>
<form action="Crud_insert_multiple" method="POST">
<input type="button" onclick ="appendRow()" value="Add Text Boxt">
<table>
<tr><td>Nama</td><td><div id='plus'></div></td></tr>
<tr>
<td></td>
<td><input type="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
这是行动表格。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Database db = (Database) getServletContext().getAttribute("db");
String[] a=request.getParameterValues("nama");
for (int i = 0; i < a.length; i++) {
try {
String sql = "insert into user(username) values('"+a[i]+"')";
db.updateSql(sql);
} catch (Exception e2) {
System.out.println(e2);
}
}
getServletContext().getRequestDispatcher("/crud_select.jsp").forward(request, response);
}
答案 0 :(得分:0)
更新输入名称以接受多个值(在数组中)
d.innerHTML += "<input type='text' name='nama[]'><br >";