add rows to table with JavaScript

后端 未结 2 570
既然无缘
既然无缘 2021-01-29 09:41

I am trying to use JavaScript or ASP C# to add rows to a table in form when the user clicks the add row button. I have working code in JavaScript. I want to add the rows with te

2条回答
  •  庸人自扰
    2021-01-29 10:20

    Here's how you could do that. (Obviously you can style the text boxes however you want.) Your code added the rows; I just added a textarea in each cell.

    function addrow() {
         var tableRef = document.getElementById('tbl').getElementsByTagName('tbody')[0];
         var rowcount = tableRef.rows.length;
         window.alert(rowcount);
         var newRow   = tableRef.insertRow(tableRef.rows.length);
         var textBox = "";
         
         // Insert a cell in the row at index 0
         var tagcell  = newRow.insertCell(0);
         var desccell = newRow.insertCell(1);
         var loccell  = newRow.insertCell(2);
         var Namecell = newRow.insertCell(3);
         var Sigcell  = newRow.insertCell(4);
    
         tagcell.innerHTML = textBox;
         desccell.innerHTML= textBox;
         loccell.innerHTML = textBox;
         Namecell.innerHTML= textBox;
         Sigcell.innerHTML = textBox;
    
      }
    Col1 Col2 col3 col4 col5

    EDIT: Your row count shows the correct number now. (It was only showing 1 each time before.)

提交回复
热议问题