[英]ReactJS - How can I add more text, textfields, selectfields upon clicking a button?
[英]How can I add a row upon clicking a button for my table?
我的桌子底部有一個按鈕。 我一直在使用 java 腳本,但我對單擊按鈕后如何在每行中添加正確數量的單元格感到困惑。
另外,有沒有辦法添加一行,同時在每行的末尾添加一組新按鈕?
您可以使用insertRow()
方法,這會創建一個空的<tr>
元素並將其添加到表格中。 insertRow()
方法在表中的指定索引處插入新行。 注意: <tr>
元素必須包含一個或多個<th>
或<td>
元素。
function myFunction() { var table = document.getElementById("data-table"); var row = table.insertRow(0); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); cell1.innerHTML = "NEW CELL1"; cell2.innerHTML = "NEW CELL2"; }
<!DOCTYPE html> <html> <head> <style> table, td { border: 1px solid black; } </style> </head> <body> <p>Click the button to add a new row at the first position of the table and then add cells and content.</p> <table id="myTable"> <tr> <td>Row1 cell1</td> <td>Row1 cell2</td> </tr> <tr> <td>Row2 cell1</td> <td>Row2 cell2</td> </tr> <tr> <td>Row3 cell1</td> <td>Row3 cell2</td> </tr> </table> <br> <button onclick="myFunction()">Try it</button> </body> </html>
這里有您需要的信息。 http://www.w3schools.com/jsref/met_table_insertrow.asp
<a href="javascript:myFunction();" title="addRow" class="btn btn-info" id="row">Add Row</a>
<script>
function myFunction() {
var table = document.getElementById("data-table");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(-1);
var cell4 = row.insertCell(-1);
var cell5 = row.insertCell(-1);
var cell6 = row.insertCell(-1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
cell3.innerHTML = "NEW CELL3";
cell4.innerHTML = "NEW CELL4";
cell5.innerHTML = "NEW CELL5";
cell6.innerHTML = "NEW CELL6";
}
</script>
我看到你正在使用 jQuery,如果你這樣做,你可以使用類似的東西:
$('#addRow').click(function(e){
e.preventDefault();
var $tr = $('<tr>').html("<td>cdcs</td><td>csc</td><td>cscs</td><td>cscs</td><td>cscs</td>");
var $tdButtons = $('<td>').html('<button>Compress</button><a href="#">Compress</a><a href="#">Download</a><a href="#">Delete</a>');
$tr.append($tdButtons);
$('#data-table').append($tr);
});
並且鏈接添加行必須類似於
<a href='#' id="addRow">Add Row</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.