Javascript 添加具有特定行类的行
我有2列和1个添加行按钮,但我想在行类“title”和“ongkir”之间添加行,但这是我的原始代码,但仍然在“ongkir”之后添加新行,请帮助我 这是我的桌子Javascript 添加具有特定行类的行,javascript,jquery,html,Javascript,Jquery,Html,我有2列和1个添加行按钮,但我想在行类“title”和“ongkir”之间添加行,但这是我的原始代码,但仍然在“ongkir”之后添加新行,请帮助我 这是我的桌子 <table class="table table-hover order-list"> <tr class="title"> <th>No</th> <th></th
<table class="table table-hover order-list">
<tr class="title">
<th>No</th>
<th></th>
</tr>
<tr>
<td>1</td>
<td><input type="button" style="width: 50px;" class="btn btn-success" id="addrow" value="+"></input></td>
</tr>
<tr class="ongkir">
<td>JNE</td>
</tr>
</table>
不
1.
JNE
这是我的javascript
$(document).ready(function () {
var counter = 2;
$("#addrow").on("click", function () {
var newRow = $("<tr>");
var cols = "";
cols += '<td>'+ counter + '</td>';
newRow.append(cols);
$("table.order-list").append(newRow);
counter++;
});
$("table.order-list").on("click", ".ibtnDel", function (event) {
$(this).closest("tr").remove();
counter -= 1
});
$(文档).ready(函数(){
var计数器=2;
$(“#添加行”)。在(“单击”上,函数(){
var newRow=$(“”);
var cols=“”;
cols+=''+计数器+'';
newRow.append(cols);
$(“table.order list”).append(newRow);
计数器++;
});
$(“table.order list”)。在(“click”、“.ibtnDel”上,函数(事件){
$(this).tr.remove();
计数器-=1
});
})) 您可以使用
insertBefore
函数代替append
,如下所示:
$(文档).ready(函数(){
var计数器=2;
$(“#添加行”)。在(“单击”上,函数(){
var newRow=$(“”);
var cols=“”;
cols+=''+计数器+'';
newRow.append(cols);
newRow.insertBefore(“tr.ongkir”);
计数器++;
});
$(“table.order list”)。在(“click”、“.ibtnDel”上,函数(事件){
$(this).tr.remove();
计数器-=1
});
});代码>
不
1.
不要使用append then。还有其他一些插入方法可以满足您的需要。检查并尝试其他方法OK谢谢你的解决方案hehehe-A-oh谢谢你解决我的问题:D我忘记删除$(“table.order list”).append(newRow);在newRow.insertBefore之后(“tr.ongkir”);对不起,我对焦不准确