无法通过jsp中的getParamNames()获得动态添加的文本框的paramNames

时间:2018-10-30 08:05:29

标签: java jsp servlets

我有一个包含3个静态行的表格,当我单击“添加行”按钮时,它将复制静态行的第一行并在4行中进行精确复制,并为动态设置了name属性还创建了行。 我的问题是我可以获取3个静态行的paramNames,但无法获取动态添加的新行的名称。请帮助我解决此问题。 jsp:

<script type="text/javascript">

function addRow(tableID) {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var colCount = table.rows[0].cells.length;

for(var i=0; i<colCount; i++) {

    var newcell = row.insertCell(i);
    if(i==1)
    {
        newcell.innerHTML = (rowCount)
        newcell.align = "center";
        newcell.style = "background-color: #C0C0C0";
    }
    else{
        newcell.innerHTML = table.rows[1].cells[i].innerHTML;

    }
    //newcell.innerHTML = table.rows[1].cells[i].innerHTML;
    //alert(newcell.childNodes);
    switch(newcell.childNodes[0].type) {
        case "text":
                newcell.childNodes[0].value = "";
                newcell.align="center";
                newcell.setAttribute("name", "header4");
                break;
        case "checkbox":
                newcell.childNodes[0].checked = false;
                newcell.align="center";
                break;
        case "select-one":
                newcell.childNodes[0].selectedIndex = 0;
                newcell.setAttribute("name", "dataType4");
                break;
    }
}
}

function regroup(i,rc,ti)
{
  for(j = (i+1);j<rc;j++)
{
  document.getElementById(ti).rows[j].cells[1].innerHTML = j+1;
  }
}
function deleteRow(tableID) {
    try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var current = '';
    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {
                alert("Cannot delete all the rows.");
                break;
            }

            table.deleteRow(i);
            rowCount--;
            i--;
          regroup(i,rowCount,tableID);
        }
    }
    }catch(e) {
        alert(e);
    }
}

</SCRIPT>
</head>
<body style="background-color: #eee">

<div align="center">
<h1>Test Data Generator</h1>
<FORM action= "/DataGenerator/formOneServlet" method="post">
<TABLE BORDER="1"  STYLE="background-color: #eee" id ="dataTable"> 
<TR STYLE="background-color:#C0C0C0">
    <TH><INPUT TYPE="button" VALUE="Delete" NAME="B2" onclick="deleteRow('dataTable')"></TH>
    <TH>S.No</TH>
    <TH>Header Name</TH>
    <TH>DataType</TH>   
</TR>
<TR>
    <TD align="center"><input type="checkbox" name="id" value=""></TD>
    <TD STYLE="background-color:#C0C0C0" align="center">1</TD>
    <TD><INPUT TYPE="TEXT" NAME="header1" SIZE="15"></TD>
    <TD>
       <select name="dataType1">
        <option value="1">Names</option>
        <option value="2">Numerical</option>
        <option value="3">PhoneNumber</option>
      </select>
    </TD>
</TR>
<TR>
    <TD align="center"><input type="checkbox" name="id" value=""></TD>
    <TD STYLE="background-color:#C0C0C0" align="center">2</TD>
    <TD><INPUT TYPE="TEXT" NAME="header2" SIZE="15"></TD>
    <TD>
      <select name="dataType2">
        <option value="1">Names</option>
        <option value="2">Numerical</option>
        <option value="3">PhoneNumber</option>
      </select>
    </TD>
</TR>
<TR>
    <TD align="center"><input type="checkbox" name="id" value=""></TD>
    <TD STYLE="background-color:#C0C0C0" align="center">3</TD>
    <TD><INPUT TYPE="TEXT" NAME="header3" SIZE="15"></TD>
    <TD>
       <select name="dataType3">
        <option value="1">Names</option>
        <option value="2">Numerical</option>
        <option value="3">PhoneNumber</option>
      </select>
    </TD>
</TR>
</TABLE>
<br>
<div align="center">
<TABLE BORDER=""  STYLE="background-color:  #eee" id = "table2"> 
<TR>
    <TH>Rows</TH>
    <TD><INPUT TYPE="button" VALUE="Add" NAME="B1" onclick="addRow('dataTable')"></TD>

    <TH> </TH>
    <TH>No of Records</TH>
    <TD><INPUT TYPE="TEXT" NAME="No_of_Records" SIZE="3"></TD>

    <TH>Area Code</TH>
    <TD><INPUT TYPE="TEXT" NAME="areaCode" SIZE="3"></TD>
</TR>
</TABLE>
</div>
<P><INPUT TYPE="SUBMIT" VALUE="Submit" NAME="B1">
<INPUT TYPE="RESET" VALUE="Reset" NAME="B1"></P>
</FORM>
</div>

用于获取paramNames的Servlet代码:

Enumeration paramNames = request.getParameterNames();
System.out.println("The Available Parameter Names are : "+paramNames.toString());
    while(paramNames.hasMoreElements()) {
        String paramName = (String)paramNames.nextElement();
        String paramValue = request.getParameter(paramName);

        System.out.println("The ParamName is :" +paramName);
        System.out.println("The ParamValue is :" +paramValue);
}

谢谢。

0 个答案:

没有答案