Javascript 如何在struts 2中的操作中获取jsp中生成的表的值?
在我的jsp中,我从一个表1中选择一些值,并使用Add按钮将其添加到另一个表2。这是我的jsp:Javascript 如何在struts 2中的操作中获取jsp中生成的表的值?,javascript,jquery,struts2,struts,struts2-jquery,Javascript,Jquery,Struts2,Struts,Struts2 Jquery,在我的jsp中,我从一个表1中选择一些值,并使用Add按钮将其添加到另一个表2。这是我的jsp: <%@page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%> <%@taglib prefix="s"uri="/struts-tags"%> <!DOCTYPE h
<%@page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="UTF-8"%>
<%@taglib prefix="s"uri="/struts-tags"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Insert title here</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
<script type="text/javascript">
$(document).ready(function(){
$('#add').on("click", function(){
$('#one tbody input:checked').parent().parent()
.appendTo("#two tbody");
return false;
});
$('#remove').on("click", function(){
$('#two tbody input:checked').parent().parent()
.appendTo("#one tbody");
return false;
});
});
function GetCellValues() {
var oTable = document.getElementById('two');
var mycars = new Array();
var mycars1 = new Array();
var mycars2 = new Array();
//gets table
var rowLength = oTable.rows.length;
//gets rows of table
for (i = 0; i < rowLength; i++){
//loops through rows
var oCells = oTable.rows.item(i).cells;
//gets cells of current row
var cellLength = oCells.length;
for(var j = 0; j < cellLength; j++){
//loops through each cell in current row
//get your cell info here
var cellVal = oCells.item(j).innerHTML;
alert(cellVal);
if(j==3){
mycars[i] = cellVal;
}
if(j==1){
mycars1[i] = cellVal;
}
if(j==2){
mycars2[i] = cellVal;
}
}
}
window.location ="DynamicTable?mytable="+mycars;
}
</script>
</head>
<body>
<button onclick="GetCellValues()">Submit</button>
<table id="one" style="border:1px solid red;">
<caption>Table 1</caption>
<thead>
<tr>
<th ></th>
<th> Id</th>
<th>Name</th>
<th>Status</th>
<th>Type</th>
<th>RollUp Type</th>
<th>System</th>
</tr>
</thead>
<tbody>
<s:iterator value="formList1">
<tr>
<td><s:checkbox theme="simple" ></s:checkbox>
</td>
<td><s:property value="id"/></td>
<td><s:property value="named"/></td>
<td><s:property value="status"/></td>
<td><s:property value="type"/></td>
<td><s:property value="rollup"/></td>
<td><s:property value="unit"/></td>
</tr>
</s:iterator>
</tbody>
</table>
<table id="two" style="border:1px solid green;">
<caption>Table 2</caption>
<thead>
<tr>
<th ></th>
<th> Id</th>
<th>Name</th>
<th>Status</th>
<th>Type</th>
<th>RollUp Type</th>
<th>System</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br><hr><br>
<button id="add">Add</button>
<button id="remove">Remove</button>
</body>
</html>
在此处插入标题
$(文档).ready(函数(){
$('#添加')。在(“单击”,函数()上){
$('#一个tbody输入:选中').parent().parent()
.附于(“#两个主体”);
返回false;
});
$('#删除')。在(“单击”,函数()上){
$('#两个tbody输入:选中').parent().parent()
.附于(“一个主体”);
返回false;
});
});
函数GetCellValues(){
var oTable=document.getElementById('two');
var mycars=新数组();
var mycars1=新数组();
var mycars2=新数组();
//获取表格
var rowLength=oTable.rows.length;
//获取表的行
对于(i=0;i
添加
去除
在这里,当我单击Struts2中的Submit按钮时,如何检索我在操作中添加到表2中的值
这就是我所尝试的,我在单击Submit按钮时调用javascript GetCellValues(),在这里我设置数组mycars、mycars1等中的每一列
在我的action类中,我有这些数组的getter和setter。但在我的行动中,我无法检索这些值
如何在操作中获取jsp中生成的表的值
更新:
我刚刚更正了行
window.location=“DynamicTable?mytable=“+mycars代码>现在我将动作中的值作为一个字符串,用逗号分隔。我想知道除了我使用的方法之外,是否还有其他方法?是的,您可以使用隐藏的输入字段将值传递给action类。
您可以在java脚本中设置这些字段,这些字段将被称为submit按钮的onclick()。使用web服务器支持的标准方法。我使用了它,但它不起作用不,您没有!如果你这么做了,我会看到的。+1因为我没有看到jquery插件的任何用途,而且看起来你解决了自己的大部分问题。。。JSP可以生成html页面。请说“如何将页面中生成的表的值传递给操作”。我们这里(struts2社区)了解struts2标记,但有一个更大的社区了解html和js/jQuery。在查看有关JS的问题时,最好只显示html。要从服务器中获取数据是另一个问题,要做到这一点,请查看jquery ajax/getJSON方法。还调查了struts2 json插件。我从来没有使用过struts2 json插件,并且一直在努力使用其他struts2 ajax和struts2 jQuery插件,如果您能向我推荐一些使用这些插件的示例链接,那将很有帮助