我有以下表格布局:
<table border="1" id="staff">
<tr><th>S/N</th><th>Company</th><th>Contact</th><th>Country</th></tr>
<tr id="1"><td>1</td><td>Futter</td><td>Anders</td<td>Germany</td></tr>
<tr id="2"><td>2</td><td>Berglunds</td><td>Christina</td<td>Sweden</td></tr>
<tr id="5"><td>3</td><td>Centro</td><td>Francisco</td<td>Mexico</td></tr>
<tr id="8"><td>4</td><td>Handel</td><td>Roland</td<td>Austria</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
现在,按下删除按钮后,一个jQuery函数将tr id张贴到正在处理的php页面,并且在成功完成php&mysqli删除操作之后,正在处理的php页面将回调消息发送到表页面。然后,jQuery函数从html表中删除相应的行:
$("body").on("click",".del",function(event){
event.preventDefault();
var table= 'staff';
var id = $(this).closest('tr').attr('id');
$.post(url,
{
table: tbl,
trid: id,
},function(data){
var callback_msg = data.return_msg;
if(callback_msg=='delete'){
$('#'+id).remove();
}
});
});
Run Code Online (Sandbox Code Playgroud)
因此,如果用户希望删除具有tr id 5的第三行,则删除该行后,S / N列将保持不变,如下所示:
<table border="1" id="staff">
<tr><th>S/N</th><th>Company</th><th>Contact</th><th>Country</th></tr>
<tr id="1"><td>1</td><td>Futter</td><td>Anders</td<td>Germany</td></tr>
<tr id="2"><td>2</td><td>Berglunds</td><td>Christina</td<td>Sweden</td></tr>
<tr id="8"><td>4</td><td>Handel</td><td>Roland</td<td>Austria</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
您可以看到序列号列现在是1,2,4(因为序列号3已删除)。
如何在我的jquery代码中包含一个函数,以便在删除后将其重新排序为1,2,3(即,最后一行的序列号将变为3)?
[注意:删除可能在中间行。因此.last()选择器在这种情况下可能无法使用]
删除该行之后,只需遍历其兄弟姐妹来更新其第一个单元格即可:
var callback_msg = data.return_msg;
if(callback_msg=='delete'){
var row = $(document.getElementById(id)); // Or continue to use the invalid ID selector: '#'+id
var siblings = row.siblings();
row.remove();
siblings.each(function(index) {
$(this).children('td').first().text(index + 1);
});
}
Run Code Online (Sandbox Code Playgroud)
实时示例 (我必须在.del每行中添加一个元素,假设您正在某处进行此操作...):
var callback_msg = data.return_msg;
if(callback_msg=='delete'){
var row = $(document.getElementById(id)); // Or continue to use the invalid ID selector: '#'+id
var siblings = row.siblings();
row.remove();
siblings.each(function(index) {
$(this).children('td').first().text(index + 1);
});
}
Run Code Online (Sandbox Code Playgroud)
$("body").on("click",".del",function(event){
event.preventDefault();
var table= 'staff';
var row = $(this).closest('tr');
var id = row.attr("id");
alert("Deleting id #" + id);
setTimeout(function() { // Simulating ajax
var siblings = row.siblings();
row.remove();
siblings.each(function(index) {
$(this).children().first().text(index);
});
}, 100);
});Run Code Online (Sandbox Code Playgroud)
.del {
cursor: pointer;
}Run Code Online (Sandbox Code Playgroud)
从评论中,您似乎不确定上面的内容。在上下文中:
$("body").on("click",".del",function(event){
event.preventDefault();
var table= 'staff';
var id = $(this).closest('tr').attr('id');
$.post(url,
{
table: tbl,
trid: id,
},function(data){
var callback_msg = data.return_msg; //
if(callback_msg=='delete'){ //
var row = $(document.getElementById(id)); // Changed
var siblings = row.siblings(); // code
row.remove(); // is
siblings.each(function(index) { // here
$(this).children('td').first().text(index + 1); //
}); //
} //
});
});
Run Code Online (Sandbox Code Playgroud)
尽管实际上,这样会更有效,并且完全避免选择id:
$("body").on("click",".del",function(event){
event.preventDefault();
var table= 'staff';
var row = $(this).closest('tr'); // *
var id = row.attr('id'); // *
$.post(url,
{
table: tbl,
trid: id,
},function(data){
var callback_msg = data.return_msg; // *
if(callback_msg=='delete'){ // *
var siblings = row.siblings(); // *
row.remove(); // *
siblings.each(function(index) { // *
$(this).children('td').first().text(index + 1); // *
}); // *
} // *
});
});
Run Code Online (Sandbox Code Playgroud)
注意有关使用'#'+id时id是一个数字:开始用数字(如ID选择#1,#42等)是无效的。jQuery 作为幕后优化的事实的副产品,孤立地支持了它们(例如,$("#1")起作用但$("#1 span")不起作用)。从理论上讲,这是无证的行为,随时可能改变。(实际上,如果他们更改了它,我无法想象它们会破坏多少个站点。:-))我避免依赖未记录的行为,所以我建议,它完全依赖于记录的行为。getElementById$(document.getElementById(id))