当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - assigning and unique name to dynamically created element

问题描述:

this is javascript code where I want to change the name attribute each time new element is created.I am creating new textbox each time when the new table row is added. but when I try to access values of these elements from servlet it shows me null value.What exactly I am doing wrong? any help would be appreciated. Thanx in advance!!

<html>

<head>

<script>

function addRow()

{

var table = document.getElementById('table');

var rowCount = table.rows.length;

var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);

var element1 = document.createElement("input");

element1.type = "text";

element1.name ="item"+rowCount;

cell1.appendChild(element1);

var cell2 = row.insertCell(1);

var element2 = document.createElement("input");

element2.type = "text";

element2.name ="amount"+rowCount;

cell2.appendChild(element2);

var cell3 = row.insertCell(2);

cell3.innerHTML = ' <input type="button" value="Edit" onclick="editRow(this)"/> <input type="button" value="Delete" onclick="deleteRow(this)"/>';

cell3.setAttribute("style", "display:none;");

var cell4 = row.insertCell(3);

cell4.innerHTML = '<input type="button" value="Save" onClick="saveRow(this)">';

document.listform.hfield.value=rowCount;

}

function submitList()

{

document.listform.submit();

}

</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Create your list</title>

</head>

<body>

<form name="listform" method="post" action="Billingservlet">

<div class="wrap">

<center>

<div id="display">

<table id='table' border="0">

<tr id='id'>

<th>Item Name</th>

<th>No. of units</th>

</tr>

</table>

<input type="button" value="Add another item" onclick="addRow()">

<input type="button" value="Submit List" onclick="submitList()">

<input type="hidden" name="hfield">

my Servlet code is

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out= response.getWriter();

int i=1;

String val1=request.getParameter("item2");

out.print(val1);

I checked what data is going to servlet using chrome:

hfield:2

Response Headersview parsed

HTTP/1.1 200 OK

server: Apache-Coyote/1.1

Content-Length: 4

Date: Fri, 13 Sep 2013 02:21:29 GMT

网友答案:

1) you can use element.name=newName; or
2) you can use element.setAttribute("name",newName);

Either of these will set the "name" attribute of your element.

so...

var element1 = document.createElement("input");
element1.name="myInput";

...or...

var element1 = document.createElement("input");
element1.setAttribute("name","myInput");
网友答案:

I think you are missing quite a bit of relevant code here. At what point does anything get posted to the servlet? Where is this code? Are you making an AJAX call to your servlet? Where is the input defined that has the name field of "item1" or "item2"? Are you sending an array of items with an AJAX post? Exactly what is going on?

Your provided JavaScript function has no call to the server, I'm not even sure how you're seeing a null value being printed out.

If you want to see all the parameters being sent to the the servlet, why not do a loop to print them out?

    for (String name : request.getParameterMap().keySet()) {
        System.out.println("Name: " + name + " = " + request.getParameter(name));
    }

Post your other JavaScript functions or related HTML forms where the data is actually being sent to the server and it might be easier to determine the problem.

分享给朋友:
您可能感兴趣的文章:
随机阅读: