[英]Adding Rows Dynamically using Javascript
Here I am adding rows dynamically in table when user clicks "AddNewRow" button. 在这里,当用户单击“ AddNewRow”按钮时,我将在表中动态添加行。 Here is the code for adding rows dynamilly,
这是动态添加行的代码,
<script type="text/javascript">
function addRow() {
var table = document.getElementById("modaltable"); //Table ID
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[1].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
//alert(newcell.childNodes);
switch (newcell.childNodes[0].type) {
case "Comment":
newcell.childNodes[i].value = "";
break;
case "DropDownList2":
newcell.childNodes[i].selectedIndex = 0;
break;
case "DropDownList1":
newcell.childNodes[i].selectedIndex = 0;
break;
}
}
}
</script>
Button code for adding rows. 用于添加行的按钮代码。 Add New Row
添加新行
When I click "Addnewrow" button it will add another row dynamically. 当我单击“ Addnewrow”按钮时,它将动态添加另一行。 But here it is adding the row but row is taking the first row value as default.
但是这里是添加行,但是row将第一行值作为默认值。 But I don't need first row selected values in another rows.
但是我不需要第一行在另一行中选择值。 Any mistake in the above code.
以上代码中的任何错误。
The new rows value is repeated because of this line 由于此行而重复了新的行值
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
new cell value is copied from existing rows cell value. 从现有行单元格值复制新的单元格值。
so just refer to this line to update the new rows cell value, like this 所以只要参考这行来更新新行的单元格值,就像这样
newcell.innerHTML = 'newVal'+(i+1)
; newcell.innerHTML = 'newVal'+(i+1)
;
If you don't need first row selected values in the new dynamically generated rows, you need to replace the following code: 如果不需要在新动态生成的行中选择第一行的值,则需要替换以下代码:
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
with 与
newcell.innerHTML = "";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.