2015-12-08 51 views
1

我有多個示例文本框。 但它只是可以使一個文本框。 我想用按鈕添加表單製作多個表單。這是我的樣品中使用多種形式添加更多表單按鈕

enter image description here

<!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); 
} 

我怎樣才能使多形式? enter image description here

回答

0

更新輸入名稱接受(數組)

d.innerHTML += "<input type='text' name='nama[]'><br >"; 
+0

以及如何動作的多個值? –

+0

我對jsp並不熟悉,但我認爲它已經期待着一組值。如果沒有,你需要做改變。 (request.getParameterValues(「nama」)將是您放入框中的一組數值)。例如,如果您有兩個帶有「Text1」和「Text2」的輸入框,則request.getParameterValues(「nama」)的javascript數組表示將爲= [「Text1」,「Text2」]。 對不起,我只是不知道如何在jsp中進行更改。 –

+0

您是否嘗試過進行此更改?不確定,但當我看到你的jsp代碼時,它似乎已經將它作爲一個數組處理。 –