如何在最后一个表格行上插入新行
how to insert new row on last table row?
我想在总价上方显示新行,以及如何在单击添加按钮时在最后一行插入上面的新行。
非常感谢!
Jquery
//for addPrice click button
$('#add_price').on('click',function(){
if($('.supplier').is(":checked") == true){
$('.addcost_table tbody').append('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');
$('table .selling_price').hide();
}else{
}
});
.HTML
<table class="addcost_table table tablesorter">
<thead>
<tr class="header_row">
<th>Item Group</th>
<th>Name</th>
<th>Code</th>
<th>Quantity</th>
<th class="cost_price">Unit Cost Price</th>
<th class="selling_price">Unit Selling Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hotel</td>
<td>Twin Room</td>
<td>TWN</td>
<td>5</td>
<td class="cost_price">100</td>
<td class="selling_price">120</td>
</tr>
<tr>
<td>Hotel</td>
<td>Single Room</td>
<td>SGL</td>
<td>1</td>
<td class="cost_price">80</td>
<td class="selling_price">100</td>
</tr>
<tr>
<td>Meals</td>
<td>Buffet Breakfast</td>
<td>BRE</td>
<td>2</td>
<td class="cost_price">10</td>
<td class="selling_price">10</td>
</tr>
<tr>
<td colspan="4">Total Price X Qty</td>
<td class="cost_price">$500</td>
<td class="selling_price">$500</td>
</tr>
</tbody>
</table>
解决这个问题的最佳方法是什么?谢谢你
$('.addcost_table > tbody:last').before('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');
你可以为此使用before
选择器,通过.addcost_table tbody tr:last
获取tbody
的最后一tr
,然后使用before
键追加tr
您的script should like this,
$('#add_price').on('click',function(){
if($('.supplier').is(":checked") == true){
$('.addcost_table tbody tr:last').before('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');
$('table .selling_price').hide();
}else{
}
});
这是示例小提琴
现有的答案声明使用 tr:last 作为选择器,.before 作为方法,对于您的要求是正确的,但我想我会把它作为替代方案:为什么不有一个包含总价格行的 tfoot 部分?
如果您想以不同的方式设置总计行的样式,这会更方便。例如,如果您想要行项目的滚动条(仅在正文上),但始终希望显示标题和页脚。如果你想要计算多少行数据(没有总数),那么这也更自然,所以你可以只得到一个tbody tr的计数,而不必减去总行。在查看CSS样式(主观)时,它也可能更容易理解。
此外,您现有的$('.addcost_table tbody').append
将按原样工作。
你也可以尝试用eq和之后喜欢
var row = $('.addcost_table>tbody>tr').length; //get the no of tr
$('.addcost_table>tbody>tr').eq(row-1); //total row -1 is above the Total price tr
.before('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');
JSFiddle
试试这个
$('.addcost_table tbody tr:last').before($('.addcost_table tbody tr:eq(0)').clone(true));
相关文章:
- 使用Ember.js将新行动态插入到Table上下文中
- 使用javascript(IE bug?)在文本区域插入新行
- Shilft + Enter 而不插入新行
- JQuery 数据表添加新行,如果该行已经存在,请将其删除并为该 ID 插入新行
- 使用 rowIndex 在单击的行下方插入新行
- 如何使用javascript或jquery创建tr的克隆并在新行中插入数据
- 在html中找到指定字符串后动态插入新行
- 如何在7个字母/字母表/数字和新行中的新字母后的键向上插入分隔符-Jquery
- 在两个单词后插入新行
- 在数组中每隔x个元素插入新行
- 剑道UI网格内联-在网格上的特定位置插入新行
- 如何在javascript中的N个字符计数后插入新行
- 单击鼠标即可从HTML表中删除旧行并插入新行
- 使用远程模型的qooxdoo表,在UI上插入新行并在服务器上更新
- 动态插入新行使用javascript,但如何保存这些多行在php/mysql
- 在插入符号处插入文本-插入新行和制表符空格
- 如何在最后一个表格行上插入新行
- 用Javascript插入新行(rowspan)
- 在Javascript中插入新行,隐藏列
- 在文本框中,当我按ENTER提交表单时,当我按shift+ENTER插入新行