简体   繁体   English

使用&#39;从HTML表中获取一行的内容 <tr onclick=alertContents()> &#39;

[英]Fetch contents of a row from HTML table using '<tr onclick=alertContents()>'

i fetched and displayed data as a table in html from mysql db using jsp and java now what i want is when a user clicks on the particular row then the data in that row should populate in 3 different tags example if my table has this data 我现在使用jsp和java从mysql db中以html的表格形式获取并显示数据,现在我想要的是,当用户单击特定行时,该行中的数据应填充在3个不同的标签示例中,如果我的表具有此数据

Name Place Mobile number 姓名地点手机号码

a         abc         123
b         def         234
c         ghi         345

(The above table is fetched from mysql db) (上表是从mysql db获取的)

if the user clicks on the 3rd Row then the data such as name place and mobile number should be displayed in 3 different tags as shown below 如果用户单击第三行,则应在3个不同的标签中显示名称地点和手机号码等数据,如下所示

Name: c
Place: ghi
Mobile: 345

thanks in advance 提前致谢

Before i used to have a button on the right side of each row with the "Name"(if it is a row of c then the button has c) on it so i dressed the button by a pic using CSS. 在我以前每行的右侧都有一个带有“名称”的按钮(如果是c的行,则按钮上有c),因此我在CSS上用图片修饰了按钮。

here goes the code i used 这是我使用的代码

  <form action="Options2" method="post">
 <table id="sorter" class="sortable" id="example" class="pretty">
 <tr>
<th>Book Id</th>
<th>Title</th>
<th>Author</th>
<th>Category</th>
<th>Status</th>            
<th>Owner</th>
<th>Borrow Date</th>
<th>Return Date</th>
<th>Requested By</th>
<th>Actions</th>            
</tr>
<%
ArrayList  rs=(ArrayList)request.getAttribute("news");
ListIterator itr=rs.listIterator();
int i=1;

while( itr.hasNext()){ 
%> 
<tr> 
<td><%=itr.next()%></td> 
<% int Id = itr.nextIndex(); %>
<td><%=itr.next()%></td>    
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td> 
<% int Id2 = itr.nextIndex(); %>
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<% 
String Bname=rs.get(Id).toString();
System.out.println(Bname); 
String Stat=rs.get(Id2).toString();
System.out.println(Stat); 
if(!Stat.equals("Not Availible"))
{
%>
<td>
<input class="buttonir" type="Submit" name="X" value="<%=Bname %>"></td>
</tr>
<% 
}
}
%>
</table>

</form>

Try this: 尝试这个:

$('table tr').click(function () {
    var BookId = $(this).children('td:eq(0)').html();
    var Title = $(this).children('td:eq(1)').html();
    var Author = $(this).children('td:eq(2)').html();

    $('div').html(
        'Book Id: ' + BookId + '<br />' +
        'Title: ' + Title + '<br />' + 
        'Author:' + Author + '<br />'
    );
});       

Demo: http://jsfiddle.net/UPxB9/1/ 演示: http//jsfiddle.net/UPxB9/1/

Compare it with your Code its almost same just few changes mentioned 将其与您的代码进行比较,几乎相同,仅提及一些更改

Only following function is added in your code and two lines more which are highlighted out of code 您的代码中仅添加了以下函数,另外两行在代码外突出显示

Edit Add following function in your (already working) javascript tag on this page or in a js file which you are using in this page 在此页面上的(已经工作的)javascript标记中或此页面中使用的js文件中, 编辑添加以下功能

function displayRowData(yourRow)
{
   var dv=document.getElementById('yourDivId');
   dv.innerHTML="<br>Name : "+ yourRow.children[0].innerHTML";
   dv.innerHTML += "<br>Place: "+yourRow.children[1].innerHTML";
   dv.innerHTML += "<br>Name : "+yourRow.children[2].innerHTML";
}

<form action="Options2" method="post">
 <table id="sorter" class="sortable" id="example" class="pretty">
 <tr>
<th>Book Id</th>
<th>Title</th>
<th>Author</th>
<th>Category</th>
<th>Status</th>            
<th>Owner</th>
<th>Borrow Date</th>
<th>Return Date</th>
<th>Requested By</th>
<th>Actions</th>            
</tr>
<%
ArrayList  rs=(ArrayList)request.getAttribute("news");
ListIterator itr=rs.listIterator();
int i=1;

while( itr.hasNext()){ 
%>

Following Line is just modified it was already in your code 以下行刚刚被修改,它已经在您的代码中

<tr onclick='displayRowData(this)'> 

<td><%=itr.next()%></td> 
<% int Id = itr.nextIndex(); %>
<td><%=itr.next()%></td>    
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td> 
<% int Id2 = itr.nextIndex(); %>
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td> 
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<% 
String Bname=rs.get(Id).toString();
System.out.println(Bname); 
String Stat=rs.get(Id2).toString();
System.out.println(Stat); 
if(!Stat.equals("Not Availible"))
{
%>
<td>
<input class="buttonir" type="Submit" name="X" value="<%=Bname %>"></td>
</tr>
<% 
}
}
%>
</table>

Following line is added to your code 将以下行添加到您的代码中

<div id='yourDivId'></div>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM