我正在尝试从引导表中获取行值,以在使用jQuery时将其发送到另一个JSP文档,但是似乎我找不到合适的方法来做到这一点。
<table class="table table-hover">
<thead>
<%
int serial = 0;
int id = 0;
String name = null;
double ammount = 0;
String date = null;
User user = null;
%>
<tr>
<th><%="Serial"%></th>
<th><%="Cost ID"%></th>
<th><%="Cost Name"%></th>
<th><%="Cost Ammount"%></th>
<th><%="Cost Date"%></th>
</tr>
</thead>
<tbody>
<%
try {
int id_id = (int) session.getAttribute("id");
List<Object[]> costs = scm.viewAll(sum.getUser(id_id));
for (Object[] cost : costs) {
id = (int) cost[0];
name = (String) cost[1];
ammount = (double) cost[2];
date = (String) cost[3];
serial += 1;
%>
<tr class="clickable-row" data-href="updatesingle">
<td><%=serial%></td>
<td><%=id%></td>
<td><%=name%></td>
<td><%=ammount%></td>
<td><%=date%></td>
</tr>
<%
}
} catch (Exception e) {
response.sendRedirect("home");
}
%>
上面的代码是表格代码。如您所见,行是可单击的,当单击行时,我将使用jQuery重定向到新页面(JSP):
jQuery(document).ready(function($) {
$(".clickable-row").click(function() {
window.document.location = $(this).data("href");
});
});
我想做的是例如将Cost ID(id变量)值传递给下一个JSP页面并以某种方式对其进行操作,并且该值应该是我单击的特定行。有办法吗?
PS我是jQuery的新手,所以请保持柔和:)
您可以将ID保存在data-
属性中:
<tr class="clickable-row" data-href="updatesingle" data-id="<%=id%>">
而且,如果要在JSP中使用该ID,则可以将其作为参数传递给另一页:
window.document.location = $(this).data("href") + "?id=" + $(this).data("id");
但是,如果要在另一页中使用jQuery来获取它,则可以将ID保存到sessionStorage中:
sessionStorage.setItem('clickedUserID', $(this).data("id"));
window.document.location = $(this).data("href");
在另一页中,您只需从sessionStorage中获取它并使用它:
var userID = sessionStorage.getItem('clickedUserID');
// do something with the ID
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句