admin管理员组

文章数量:1022226

I have a javascript function that I need to call on click of the delete button -

JavaScript:

function test(var1) {
    alert(var1);
}

JSP code (logs is an ArrayList of length 10):

<table>
<%
for(int i=0; i<len; i++)
{
%>
    <tr>
    <td><%out.println(logs[i].getItemName());%></td>
    <td><%out.println(logs[i].getItemDesc()); %></td>
    <td> <input class="submit" type="submit" value="Delete" onclick="test(<%logs[i].getItemName();%>);"/></td>
    </tr>

<%
}
%>
</table>

This will print the output as -

Name1        Description1       Delete_Button

Name2        Description2       Delete_Button

.

.

Name10        Description10     Delete_Button

I am not getting how to send the correct itemname value to the JavaScript, when I click on a corresponding row's delete button. Currently I am getting the value as undefined in the JavaScript alert when I click any of the delete buttons.

I have a javascript function that I need to call on click of the delete button -

JavaScript:

function test(var1) {
    alert(var1);
}

JSP code (logs is an ArrayList of length 10):

<table>
<%
for(int i=0; i<len; i++)
{
%>
    <tr>
    <td><%out.println(logs[i].getItemName());%></td>
    <td><%out.println(logs[i].getItemDesc()); %></td>
    <td> <input class="submit" type="submit" value="Delete" onclick="test(<%logs[i].getItemName();%>);"/></td>
    </tr>

<%
}
%>
</table>

This will print the output as -

Name1        Description1       Delete_Button

Name2        Description2       Delete_Button

.

.

Name10        Description10     Delete_Button

I am not getting how to send the correct itemname value to the JavaScript, when I click on a corresponding row's delete button. Currently I am getting the value as undefined in the JavaScript alert when I click any of the delete buttons.

Share Improve this question edited Oct 30, 2012 at 7:23 Egon 4,7871 gold badge25 silver badges38 bronze badges asked Oct 30, 2012 at 6:58 StudentStudent 4,5638 gold badges29 silver badges32 bronze badges 1
  • Apart from the JavaScript part: Do you really use JSP code like that? Have you ever heard of <c:foreach/>? I would avoid <% %> in all my code. – Michael Piefel Commented Oct 30, 2012 at 7:20
Add a ment  | 

1 Answer 1

Reset to default 5
onclick='test("<%=logs[i].getItemName();%>");'

I have a javascript function that I need to call on click of the delete button -

JavaScript:

function test(var1) {
    alert(var1);
}

JSP code (logs is an ArrayList of length 10):

<table>
<%
for(int i=0; i<len; i++)
{
%>
    <tr>
    <td><%out.println(logs[i].getItemName());%></td>
    <td><%out.println(logs[i].getItemDesc()); %></td>
    <td> <input class="submit" type="submit" value="Delete" onclick="test(<%logs[i].getItemName();%>);"/></td>
    </tr>

<%
}
%>
</table>

This will print the output as -

Name1        Description1       Delete_Button

Name2        Description2       Delete_Button

.

.

Name10        Description10     Delete_Button

I am not getting how to send the correct itemname value to the JavaScript, when I click on a corresponding row's delete button. Currently I am getting the value as undefined in the JavaScript alert when I click any of the delete buttons.

I have a javascript function that I need to call on click of the delete button -

JavaScript:

function test(var1) {
    alert(var1);
}

JSP code (logs is an ArrayList of length 10):

<table>
<%
for(int i=0; i<len; i++)
{
%>
    <tr>
    <td><%out.println(logs[i].getItemName());%></td>
    <td><%out.println(logs[i].getItemDesc()); %></td>
    <td> <input class="submit" type="submit" value="Delete" onclick="test(<%logs[i].getItemName();%>);"/></td>
    </tr>

<%
}
%>
</table>

This will print the output as -

Name1        Description1       Delete_Button

Name2        Description2       Delete_Button

.

.

Name10        Description10     Delete_Button

I am not getting how to send the correct itemname value to the JavaScript, when I click on a corresponding row's delete button. Currently I am getting the value as undefined in the JavaScript alert when I click any of the delete buttons.

Share Improve this question edited Oct 30, 2012 at 7:23 Egon 4,7871 gold badge25 silver badges38 bronze badges asked Oct 30, 2012 at 6:58 StudentStudent 4,5638 gold badges29 silver badges32 bronze badges 1
  • Apart from the JavaScript part: Do you really use JSP code like that? Have you ever heard of <c:foreach/>? I would avoid <% %> in all my code. – Michael Piefel Commented Oct 30, 2012 at 7:20
Add a ment  | 

1 Answer 1

Reset to default 5
onclick='test("<%=logs[i].getItemName();%>");'

本文标签: javaPassing parameters to Javascript from jspStack Overflow