2016-02-26 52 views
1

我目前正在使用選擇框動態添加表格行並獲取servlet中添加的選擇框的值。選擇框的添加工作正常,但它很好,但是當我獲得在servlet中動態添加的選擇框的值時,值將變爲空值。這是我到現在爲止代碼:Java servlet無法讀取html選擇標記名稱

的Javascript:

function addCreditRow(tableID) {    
    var table = document.getElementById(tableID);    
    var rowCount = table.rows.length; 
    var row = table.insertRow(rowCount); 
    var count = rowCount+1; 

    var cell1 = row.insertCell(0); 
    var element1 = document.createElement("select"); 
    element1.name = "credAcc"+count; //*** EDIT *** 
    <%for(int i = 0; i < aList.size(); i++){%> 
     var option = document.createElement("option"); 
     option.setAttribute("value", "<%=aList.get(i).getAccount_name()%>"); 
     option.innerHTML = "<%=aList.get(i).getAccount_name()%>"; 
     element1.appendChild(option); 
     <%}%> 
    cell1.appendChild(element1); 
    document.getElementById("cred").value=count; 

    var cell2 = row.insertCell(1);    
    var element = document.createElement("input");    
    element.type = "text"; 
    element.name = "credAmt"+count;  //*** EDIT *** 
    cell2.appendChild(element); 
} 

HTML:

<div class="pull-right"> 
<INPUT type="button" class="btn btn-default" value="Add A Credit Account" onclick="addCreditRow('credTable')" /> 
             </div> 
<input type="hidden" name="credit" id="cred" value="1"> 
              <br><br> 

              <table class="table"> 
              <thead> 
              <tr> 
               <th>Account</th> 
               <th>Amount</th> 
              </tr> 
               </thead> 
              </table> 
               <table class="table" id="credTable"> 
               <tbody> 
              <tr> 

                 <td><select name="credAcc1"> 
                <%for(int i = 0 ; i < aList.size(); i++){%> 
                <option value="<%=aList.get(i).getAccount_name()%>"><%=aList.get(i).getAccount_name()%></option> 
                <%}%> 
                </select></td> 
               <td><input type="text" name="credAmt1"></td> 
              </tr> 
               </tbody> 
              </table> 

的Java Servlet:

int credit = Integer.parseInt(request.getParameter("credit")); 
     ArrayList<String> credList = new ArrayList(); 

     for(int x = 1; x < credit+1; x++){ 
      credList.add(request.getParameter("credAcc"+x)); 
     } 

     ServletContext context= getServletContext(); 
     request.setAttribute("credList", credList); 
      RequestDispatcher rd= context.getRequestDispatcher("/newjsp.jsp"); 
      rd.forward(request, response); 

JSP頁面,在這裏我給的值存儲在ArrayList中:

<%@page import="java.util.ArrayList"%> 
<HTML> 
<% 

ArrayList<String> credList = (ArrayList<String>) request.getAttribute("credList"); 
%> 
<HEAD>  
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>  


</HEAD> 

<BODY> 

<form name="myform" action="myServlet" method="post"> 



    <h1>CREDIT ACCOUNTS:</h1><br> 
    <%for(int i = 0; i < credList.size(); i++){%> 
    <h2><%=credList.get(i)%></h2><br> 
    <%}%> 

</form> 

</BODY> 
</HTML> 

我的javascript函數有問題嗎?

+0

你是什麼意思'值變爲空。 '。在你的html中,或者當你在servlet中獲取值時? – MaVRoSCy

+0

您將數據發佈到servlet的代碼在哪裏? – MaVRoSCy

+0

@MaVRoSCy queston編輯。我已經添加了jsp代碼,我在代碼ArrayList credList =(ArrayList )request.getAttribute(「credList」)中顯示arraylist – user3418987

回答

0

不完全理解你的問題,但是你有一個很好的可能性,你的意思是在你的代碼中沒有在你的HTML中顯示的JSP scriptlet中添加選項。如果是這樣的話,你創建的方式選項可這樣一來你的addCreditRow功能:

try{ 
    t1.add(new Option(name , value )); 
}catch(e){ 
    t1.add(new Option(name , value), null); 
} 

這樣做是試圖用標準的兩個參數的第一個新的運營商創建選項,如果失敗,該它將爲一些瀏覽器,將創建三個參數。