1
我有多个示例文本框。 但它只是可以使一个文本框。 我想用按钮添加表单制作多个表单。这是我的样品中使用多种形式添加更多表单按钮
<!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">
\t <input type="button" onclick ="appendRow()" value="Add Text Boxt">
\t <table>
\t <tr><td>Nama</td><td><div id='plus'></div></td></tr>
\t <tr>
\t <td></td>
\t <td><input type="submit" value="Save"></td>
\t </tr>
\t </table>
\t </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);
}
以及如何动作的多个值? –
我对jsp并不熟悉,但我认为它已经期待着一组值。如果没有,你需要做改变。 (request.getParameterValues(“nama”)将是您放入框中的一组数值)。例如,如果您有两个带有“Text1”和“Text2”的输入框,则request.getParameterValues(“nama”)的javascript数组表示将为= [“Text1”,“Text2”]。 对不起,我只是不知道如何在jsp中进行更改。 –
您是否尝试过进行此更改?不确定,但当我看到你的jsp代码时,它似乎已经将它作为一个数组处理。 –