问题描述:

I'll try to make a brief explanation, explaining in a clear and exhaustive the problem.

I have a JSP page that is invoked by a servlet. This JSP page generate a number of buttons according to the number of records returned by the servlet after a query (these records are passed through an ArrayList object, so if the ArrayList.size() it's 3, I'll generate 3 buttons; if 10, 10 buttons etc.), each button is positioned next to one another in a table and each row can have a maximum of 5 buttons.

Now the real question is: once I created these buttons, I need to call a servlet, but this servlet must know which button was pressed (For example, if there are 5 buttons, if I press the 3rd, I need to send to the servlet that I pressed the 3rd botton). How could I implement this? Could I do it through scripts in JavaScript? If possible, could you share me the code necessary for this purpose?

Notes: the number of buttons is DYNAMIC, because the number of buttons varies every time. I can press just one button and the servlet must know which button was pressed. (If I press the 1th button, then return "1". If I press the 2nd button, then return "2". If I press the n° button, then return "n". I hope it is clear, now.

I hope I was clear to explain the problem, but I paste the part of the code relating to the generation of the buttons:

<table class="table" cellspacing="0" cellpadding="0">

<%

int buttons_number = 0;

int index = 0;

while(buttons_number< array.size()){

%>

<tr>

<%

while(index < 5 && buttons_number < array.size()){

String idChar = Integer.toString(index);

%>

<td><input type="button" class="MyButton" id="<%=idChar %>"></td>

<%

index++;

buttons_number++;

}

index= 0;

%>

</tr>

<%

}

%>

</table>

网友答案:

Solution:

I've changed:

<td><input type="button" class="MyButton" id="<%=idChar %>"></td>

Into:

<td><input type="submit" class="MyButton" id="MyButton" name="MyButton" value="<%=idChar %>"></td>

Then, when I will press a submit button I will call the doPost() method in the servlet and send the value of the button pressed.

String ButtonPressed = request.getParameter("MyButton");
相关阅读:
Top