Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在多个TDs中添加分类账总计_Javascript_Jquery_Sum_Spreadsheet - Fatal编程技术网

Javascript 在多个TDs中添加分类账总计

Javascript 在多个TDs中添加分类账总计,javascript,jquery,sum,spreadsheet,Javascript,Jquery,Sum,Spreadsheet,此代码生成一个分类账。我把范围缩小到最小。通过单击加号,它会在分类账中添加一行 我希望添加变量newAmount的每个总数,并将其更新后的总数定位在每行右侧的TD中。我创建了newAmount.id=mainmount;创建唯一的ID,认为这会有所帮助 var mainNumber = 0; function addElement() { //add a number for each row mainNumber++; //create each row, id each

此代码生成一个分类账。我把范围缩小到最小。通过单击加号,它会在分类账中添加一行

我希望添加变量newAmount的每个总数,并将其更新后的总数定位在每行右侧的TD中。我创建了newAmount.id=mainmount;创建唯一的ID,认为这会有所帮助

var mainNumber = 0;

function addElement()
{

  //add a number for each row
  mainNumber++;


  //create each row, id each slot, and add it where it goes
  newDiv = document.createElement("div");
  newDiv.id = "main";
  newTable = document.createElement("table");
  newTable.id = "mainTable";
  newDiv.appendChild(newTable);
  newTr = document.createElement("tr")
  newTr.id = (mainNumber);
  newTr.className = "mainRow";
  newTable.appendChild(newTr);
  newAmount = document.createElement("td");
  newAmount.id = "mainAmount";
  newAmount.className = (mainNumber);
  newPlus = document.createElement("td");
  newPlus.id = "mainPlus";
  newTotalTable = document.createElement("table");
  newDiv.appendChild(newTotalTable);
  newTotalTable.id = "mainTotalTable";
  newTotalTr = document.createElement("tr");
  newTotalTable.appendChild(newTotalTr);
  newTotalTr.id = "mainTotalTr";
  newTotalTd = document.createElement("td");
  newTotalTd.id = "mainTotalTd" + (mainNumber);
  newTr.appendChild(newAmount);
  newTotalTr.appendChild(newTotalTd);

  //whats default inside of each slot
  newAmount.innerHTML = '<form name="formAmount"><textarea name="textAmount" size="25" onfocus="wait();" id="amount' + (mainNumber) + '">0</textarea>';
  newTr.appendChild(newPlus);

  //click this to add a row
  newPlus.innerHTML = '<a href="#" onclick="addElement();"><img src="images/plus.png"></a>';


  // add the newly created element and it's content into the DOM
  my_div = document.getElementById("mainAnchor");
  document.body.insertBefore(newDiv, my_div);

}

//doesn't work...trying to hover over any row and show var idName in console  
function trHover(){ 
$('tr').hover(function() {
    var idName = $('tr'+'#'+(mainNumber)).attr('id');                    
   console.log(idName);
});
}

//when you focus on amount box, this is activated, which adds attribute onblur and stars addTotal
function wait(){
var blurred = $(this).attr("onblur");
blurred = addTotal();
}

//adds total and displays it in td to the right
function addTotal(){
var y = 1;
var sum = 0;
var input;
while( ( input = document.getElementById( 'amount'+y ) ) ) {
    sum += parseInt( input.value );
    ++y;
    console.log(sum);
    $("#mainTotalTd1").text(sum);
}
}

与其添加一行,不如单击加号添加一个div和两个表,因此我不确定您要在那里添加什么。不过,我可以修复悬停功能:

$('tr').hover(function() {
    var idName = $(this).attr('id'); // 'this' is the element being hovered
    console.log(idName);
});