我有一个jsp文件,我从表格的表格中检索数据库中的数据。我的jsp如下:将不同的元素传递给一个具有相同id的Javascript函数
<%
ResultSet rs1 = stmt.executeQuery("select * from book where category='Fiction'");
%>
<h1><font size="3">
<form name="f1" id="f1" method="post">
<table border="1">
<tr>
<th>Name of the Book</th>
<th>Author</th>
<th>ISBN</th>
<th>Price</th>
<th>Publication</th>
<th>Description</th>
</tr>
<%
while(rs1.next())
{
%>
<tr>
<td id="bname"><%= rs1.getString(1) %></td>
<td id="aname"><%= rs1.getString(2) %></td>
<td id="isbn"><%= rs1.getString(3) %></td>
<td id="price"><%= rs1.getString(4) %></td>
<td id="pname"><%= rs1.getString(5) %></td>
<td id="desc"><%= rs1.getString(7) %></td>
<td><input type="button" value="Add to Cart" onclick="cart()"></input></td>
</tr>
<%
}
%>
这将以行数为输出。当我点击'Add to Cart'按钮时,它应该将单元格元素传递给javascript函数,从而通过JS函数传递给servlet。
我在这里面临的问题是,由于每个'td'的id都是相同的,虽然我在表格的第二行单击,但只有第一行的元素传递到javascript函数。
这里是我的javascript函数:
function cart()
{
var bname = window.document.getElementById("bname").textContent;
var isbn = window.document.getElementById("isbn").textContent;
var price = window.document.getElementById("price").textContent;
alert(bname);
alert(isbn);
alert(price);
}
上述“警报只虽然我点击出现在第二行中的按钮的正在显示第一行的值。
我在网上尝试了很多选择,但无法解决问题。
两个元素不能有相同的'ID '。文档中的'id'必须是唯一的。 – 2013-03-21 14:36:46
它是正确的。我也以这种方式尝试过。我声明了一个变量'i'并按如下方式使用它: int i = 0; %>