我遇到的问题是我的功能calculate()
。该程序的目的是使用 JavaScript 和 html 创建动态电子表格,并使用 CSS 进行装饰。
到目前为止,我已经将值插入到电子表格中,但是当尝试对其进行简单的加法计算时,我从单元格中获取的值没有被正确地获取。
这是给我带来问题的具体功能。我为草率的代码道歉。任何帮助将不胜感激,我确信这是我所缺少的一些简单的东西。
编辑:: 更正了问题,对于那些想知道的人,我不得不调用 not innerHTML.text 或 value。它必须只是相关单元格上的 .innerHTML 。
function calculate() {
//create two variables to get the values in the row and columns
var row1, row2, col1, col2, total, totalInsert;
var row = document.getElementById("row");
var col = document.getElementById("col");
var value = document.getElementById(row + "_" + col);
var formula = document.getElementById("inputText");
formula = formula.value; //=SUM(1,2,2,1)
formula1 = formula;
//get the raw values to ints
row = row.value;
col = col.value;
//get JUST the formula in questions part
formula = formula.substr(0,4);
//Parsing the the selected cells from =sum(1,1,2,1) into Cell ID's
col1 = parseInt(formula1.substr(5, 1));//row 1
row1 = parseInt(formula1.substr(7, 1));
col2 = parseInt(formula1.substr(9, 1));
row2 = parseInt(formula1.substr(11, 1)); // column2
//this gives us the proper cell's address. id=1_1
var td1 = col1 + "_" + row1;
var td2 = col2 + "_" + row2;
//problem starts around here
var sum = document.getElementById(td1);
var sum2 = document.getElementById(td2);
//this returns a undefined value
sum = sum.value;
sum2 = sum2.value;
//this restults in a NaN
sum = parseInt(sum);
sum2 = parseInt(sum2);
//creating the total value
total = sum + sum2;
//returning values
totalInsert = document.getElementById(td1).innerHTML = total;
}