我在PHP中编写了很多代码但在javascript中编写的代码不多。此脚本编写为自总矩阵,以便总计行和列。现在它在IE7上的兼容模式下工作,但不是。我会很感激对代码的更多学习回顾 - 我猜它很简单,但我还没弄明白......
我意识到这是一些代码 - 我试图将其格式化以便于审查 - 但我真的不确定是什么。老实说,直到我改变了兼容性设置,并意识到它在其他地方都被软化了,我才认为这一切都很好......
提前感谢您的好意 - 很乐意回答任何问题或提供更多信息!
//declare variables
var sq = new Array();
for (i = 0; i < 50; i++) {
sq[i] = new Array();
}
var totTot = new Array();
var curCode = new Array();
var curCol = new Array();
var curRow = new Array();
var curVal = new Array();
//following function calculates first the column totals, then the row totals
//based and finally the intersection of the column and row totals
function calculate(cols, cell, col, row) {
//verify numeric
var numericExpression = /^([1-9][0-9]*(\.[0-9]+)?|0\.[0-9]+|\.[0-9]+|0)$/;
if (cell.match(numericExpression)) {
sq[col][row] = cell;
}
else {
sq[col][row] = 0;
document.getElementById("c" + col + "-r" + row).value = "";
}
var colTot = 0;
var rowTot = 0;
var total = 0;
for (r = 0; r < 14; r++) {
if (sq[col][r] > 0) {
colTot += (sq[col][r]) * 1;
} //set column total
else {
colTot += 0;
}
if (colTot > 0) {
//write the column total
document.getElementById('col' + col).innerHTML = colTot;
}
//if 0 don't write anything
else {
document.getElementById('col' + col).innerHTML = '';
}
}
for (c = 0; c < cols; c++) {
if (sq[c][row] > 0) { //set row total
rowTot += (sq[c][row]) * 1;
}
else {
rowTot += 0;
}
if (rowTot > 0) {
//write the row total
document.getElementById('row' + row).innerHTML = rowTot;
}
//nothing if 0
else {
document.getElementById('row' + row).innerHTML = '';
}
totTot[row] = rowTot * 1;
}
for (t = 0; t < 14; t++) {
if (totTot[t] > 0) {
total += (totTot[t]) * 1;
}
else {
total += 0;
}
}
if (total > 0) {
//write total at intersection of row/column total
document.getElementById('totals').innerHTML = total;
}
else {
document.getElementById('totals').innerHTML = '';
}
}
//此函数将现有值(先前输入的值)置于网格中并运行“计算”功能
function getCurValues(cols) {
for (var a = 0; a < curCode.length; a++) {
var nameVar = "_" + curCode[a] + "-" + curRow[a];
document.getElementById(nameVar).value = curVal[a];
calculate(cols, curVal[a], curCol[a], curRow[a]);
}
//以下是包含先前数据的数组
curCode[0]='1035';
curVal[0]=5;
curRow[0]=5;
curCol[0]=3;
//这里有几行HTML:
<table><tr>
<td><input name='_1000-0' type='text' id='c0-r0' class='input' onblur='calculate(3,value,0,0)' size='3' /></td>
<td><input name='_1004-0' type='text' id='c1-r0' class='input' onblur='calculate(3,value,1,0)' size='3' /></td>
<td><input name='_1005-0' type='text' id='c2-r0' class='input' onblur='calculate(3,value,2,0)' size='3' /></td>
<td class='rowTot' width='25px'><div id='row0'></div></td>
</tr>
<tr>
<td><input name='_1000-1' type='text' id='c0-r1' class='input' onblur='calculate(3,value,0,1)' size='3' /></td>
<td><input name='_1004-1' type='text' id='c1-r1' class='input' onblur='calculate(3,value,1,1)' size='3' /></td>
<td><input name='_1005-1' type='text' id='c2-r1' class='input' onblur='calculate(3,value,2,1)' size='3' /></td>
<td class='rowTot' width='25px'><div id='row1'></div></td>
</tr>
<tr>
<td class='colTot'><div id='col0'></div></td>
<td class='colTot'><div id='col1'></div></td>
<td class='colTot'><div id='col2'></div></td>
<td class='totTot'><div id='totals'><div></td>
</tr></table>