[英]Adding a table row in jQuery
I'm using jQuery to add an additional row to a table as the last row.我正在使用 jQuery 将附加行添加到表中作为最后一行。
I have done it this way:我是这样做的:
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以添加到这样的表中的内容是否有限制(例如输入、选择、行数)? Is there a different way to do it?
有不同的方法吗?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
The answers above are very helpful, but when student refer this link to add data from form they often require a sample.上面的答案非常有帮助,但是当学生参考此链接从表单添加数据时,他们通常需要一个样本。
I want to contribute an sample get input from from and use .after()
to insert tr to table using string interpolation
.我想贡献一个样本从 from 获取输入并使用
.after()
使用字符串interpolation
将 tr 插入到表中。
function add(){
let studentname = $("input[name='studentname']").val();
let studentmark = $("input[name='studentmark']").val();
$('#student tr:last').after(`<tr><td>${studentname}</td><td>${studentmark}</td></tr>`);
}
function add(){ let studentname = $("input[name='studentname']").val(); let studentmark = $("input[name='studentmark']").val(); $('#student tr:last').after(`<tr><td>${studentname}</td><td>${studentmark}</td></tr>`); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> </head> <body> <form> <input type='text' name='studentname' /> <input type='text' name='studentmark' /> <input type='button' onclick="add()" value="Add new" /> </form> <table id='student'> <thead> <th>Name</th> <th>Mark</th> </thead> </table> </body> </html>
What is the best method in jQuery to add an additional row to a table as the last row? jQuery在表中添加最后一行作为最后一行的最佳方法是什么?
Is this acceptable?这可以接受吗?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?您可以向像这样的表中添加什么内容(例如输入,选择,行数)是否有限制?
If you want to add row
before the <tr>
first child.如果要在
<tr>
第一个孩子之前添加row
。
$("#myTable > tbody").prepend("<tr><td>my data</td><td>more data</td></tr>");
If you want to add row
after the <tr>
last child.如果要在
<tr>
最后一个孩子之后添加row
。
$("#myTable > tbody").append("<tr><td>my data</td><td>more data</td></tr>");
Add tabe row using JQuery:使用 JQuery 添加表格行:
if you want to add row after last of table's row child , you can try this如果你想在表的最后一行孩子之后添加行,你可以试试这个
$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');
if you want to add row 1st of table's row child , you can try this如果你想添加表的第 1 行子行,你可以试试这个
$('#myTable tr').after('<tr>...</tr><tr>...</tr>');
Try This:尝试这个:
// Using jQuery - append $('#myTable > tbody').append('<tr><td>3</td><td>Smith Patel</td></tr>'); // Using jQuery - appendTo $('<tr><td>4</td><td>J. Thomson</td></tr>').appendTo("#myTable > tbody"); // Using jQuery - add html row let tBodyHtml = $('#myTable > tbody').html(); tBodyHtml += '<tr><td>5</td><td>Patel S.</td></tr>'; $('#myTable > tbody').html(tBodyHtml); // Using jQuery - after $('#myTable > tbody tr:last').after('<tr><td>6</td><td>Angel Bruice</td></tr>'); // Using JavaScript - insertRow const tableBody = document.getElementById('myTable').getElementsByTagName('tbody')[0]; const newRow = tableBody.insertRow(tableBody.rows.length); newRow.innerHTML = '<tr><td>7</td><td>K. Ashwin</td></tr>';
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="myTable"> <thead> <tr> <th>Id</th> <th>Name</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John Smith</td> </tr> <tr> <td>2</td> <td>Tom Adam</td> </tr> </tbody> </table>
Pure JS is quite short in your case在您的情况下,纯 JS 很短
myTable.firstChild.innerHTML += '<tr><td>my data</td><td>more data</td></tr>'
function add() { myTable.firstChild.innerHTML+=`<tr><td>date</td><td>${+new Date}</td></tr>` }
td {border: 1px solid black;}
<button onclick="add()">Add</button><br> <table id="myTable"><tbody></tbody> </table>
(if we remove <tbody>
and firstChild
it will also works but wrap every row with <tbody>
) (如果我们删除
<tbody>
和firstChild
它也可以工作,但用<tbody>
包裹每一行)
I have tried the most upvoted one, but it did not work for me, but below works well.我尝试了最受好评的一个,但它对我不起作用,但下面的效果很好。
$('#mytable tr').last().after('<tr><td></td></tr>');
Which will work even there is a tobdy there.即使那里有一个 tobdy 也能工作。
这也可以做到:
$("#myTable > tbody").html($("#myTable > tbody").html()+"<tr><td>my data</td><td>more data</td></tr>")
To add a new row at the last of current row, you can use like this要在当前行的最后添加一个新行,您可以像这样使用
$('#yourtableid tr:last').after('<tr>...</tr><tr>...</tr>');
You can append multiple row as above.您可以像上面那样追加多行。 Also you can add inner data as like
你也可以像这样添加内部数据
$('#yourtableid tr:last').after('<tr><td>your data</td></tr>');
in another way you can do like this以另一种方式你可以这样做
let table = document.getElementById("tableId");
let row = table.insertRow(1); // pass position where you want to add a new row
//then add cells as you want with index
let cell0 = row.insertCell(0);
let cell1 = row.insertCell(1);
let cell2 = row.insertCell(2);
let cell3 = row.insertCell(3);
//add value to added td cell
cell0.innerHTML = "your td content here";
cell1.innerHTML = "your td content here";
cell2.innerHTML = "your td content here";
cell3.innerHTML = "your td content here";
Here, You can Just Click on button then you will get Output.在这里,您只需单击按钮即可获得输出。 When You Click on Add row button then one more row Added.
当您单击“添加行”按钮时,又添加了一行。
I hope It is very helpful.我希望它非常有帮助。
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<style>
table {
margin: 25px 0;
width: 200px;
}
table th, table td {
padding: 10px;
text-align: center;
}
table, th, td {
border: 1px solid;
}
</style>
</head>
<body>
<b>Add table row in jQuery</b>
<p>
Click on the button below to
add a row to the table
</p>
<button class="add-row">
Add row
</button>
<table>
<thead>
<tr>
<th>Rows</th>
</tr>
</thead>
<tbody>
<tr>
<td>This is row 0</td>
</tr>
</tbody>
</table>
<!-- Script to add table row -->
<script>
let rowno = 1;
$(document).ready(function () {
$(".add-row").click(function () {
rows = "<tr><td>This is row "
+ rowno + "</td></tr>";
tableBody = $("table tbody");
tableBody.append(rows);
rowno++;
});
});
</script>
</body>
</html>
)Daryl: )达里尔:
You can append it to the tbody using the appendTo method like this:您可以使用 appendTo 方法将其附加到 tbody,如下所示:
$(() => {
$("<tr><td>my data</td><td>more data</td></tr>").appendTo("tbody");
});
You'll probably want to use the latest JQuery and ECMAScript.您可能想要使用最新的 JQuery 和 ECMAScript。 Then you can use a back-end language to add your data to the table.
然后,您可以使用后端语言将数据添加到表中。 You can also wrap it in a variable like so:
您也可以将其包装在一个变量中,如下所示:
$(() => {
var t_data = $('<tr><td>my data</td><td>more data</td></tr>');
t_data.appendTo('tbody');
});
var html = $('#myTableBody').html();
html += '<tr><td>my data</td><td>more data</td></tr>';
$('#myTableBody').html(html);
or或者
$('#myTableBody').html($('#myTableBody').html() + '<tr><td>my data</td><td>more data</td></tr>');
TIP: Inserting rows in html table
via innerHTML or .html()
is not valid in some browsers (similar IE9
), and using .append("<tr></tr>")
is not good suggestion in any browser.提示:通过
innerHTML or .html()
在html table
插入行在某些浏览器(类似于IE9
)中无效,并且在任何浏览器中使用.append("<tr></tr>")
都不是好建议。 best and fastest way is using the pure javascript
codes.最好和最快的方法是使用
pure javascript
代码。
for combine this way with jQuery
, only add new plugin similar this to jQuery
:要以这种方式与
jQuery
结合,只需添加类似于jQuery
新插件:
$.fn.addRow=function(index/*-1: add to end or any desired index*/, cellsCount/*optional*/){
if(this[0].tagName.toLowerCase()!="table") return null;
var i=0, c, r = this[0].insertRow((index<0||index>this[0].rows.length)?this[0].rows.length:index);
for(;i<cellsCount||0;i++) c = r.insertCell(); //you can use c for set its content or etc
return $(r);
};
And now use it in whole the project similar this:现在在整个项目中使用它,类似于:
var addedRow = $("#myTable").addRow(-1/*add to end*/, 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.