【发布时间】:2014-04-30 07:30:51
【问题描述】:
我制作了一个表格,将表格的行和列相加。但现在我想计算 Servlet 中的总和。我将带有 JSON 的列和行数组发送到 Servlet。一切正常。但是现在的问题是,如何计算servlet中的行和列的总和并发送回来?
这是我的代码:
$(document).on('change',function(){
var columnValues={}, rowValues={};
$("#sum_table tr").each(function(rowIndex){
$("td input", $(this)).each(function(colIndex){
var value=$(this).val();
// indexes need +1 to get the row number, because
// the indexes are 0-based.
if (undefined===columnValues[colIndex+1]){
columnValues[colIndex+1]=[];
}
if (undefined===rowValues[rowIndex+1]){
rowValues[rowIndex+1]=[];
}
rowValues[rowIndex+1].push(value);
columnValues[colIndex+1].push(value);
});
});
// send data to server
$.ajax({
url: 'ServletPost',
type: 'post',
data: {rows:rowValues, columns:columnValues},
dataType: 'json',
success: function(data){
// insert your server-calculated data to dom
var rows = data.rows,
columns = data.columns;
// insert your server-calculated data to dom
$("td.total").each(function(rowIndex){
$(this).text(rows[rowIndex+1]);
});
$("tr.totalCol td").each(function(columnIndex){
$(this).text(columns[columnIndex+1]);
});
}
});
});
提前感谢您!
【问题讨论】: