删除 JQuery 中的表行
我在根据复选框从表中删除行时遇到问题。下面是我所做的屏幕截图。这是一张航空航班详细信息的表格。
最多可以添加 15 行。我想要做的是通过选择所有复选框并按删除行,授予用户删除特定航班或删除所有航班的权限。
以下是用于处理此问题的 jquery:
$("input[id$='_del_flight']").click(function() {
var n = $("#" + this.id.slice(0,1) + "_airline_table tr").length;
$("#" + this.id.slice(0,1) + "_airline_table tr").each(function(){
if ( $("input[type=checkbox]",this).is(':checked') ) {
if ( n > 2 ) {
$(this).remove();
n--;
//alert("Value n-- is: " + n);
} else {
$(this).find("input").val('');
$(this).find("input[type=checkbox]").removeAttr("checked");
$(this).find('option:first').attr('selected', 'selected').parent('select');
}
}
});
});
这个 jquery 很好地实现了我想要的功能。用户可以选择任何特定的航班详细信息并将其删除,或者用户可以将其全部删除。
但是,当用户决定将它们全部删除时,我的 jquery 将删除从第 2 行开始的行(上图所示的是 Flight 1)。我需要想出另一种方法,必须从最后一行删除该行,即 Flight 15,然后是 Flight 14,Flight 13 等等。我将通过将该行的所有输入值设为空来保留行号 2(由航班 1 使用)。
我确实尝试使用 jquery :gt 选择器,它不像我想要的那样工作。因此,如果有人可以帮助我删除最后一行中的行(删除全部)并保留用户可以根据用户选择删除特定航班的功能,我将非常感激。
非常感谢您的帮助。
I have problem removing rows from a table according to the checkbox. Below is the screen shot for what I do. It's a table for air flight details.
The maximum row can be added is 15 rows. What I want to do is to give the user access to delete which particular flight or just delete all, by selecting all checkbox and press Delete Row.
The following is the jquery that use to handle this:
$("input[id$='_del_flight']").click(function() {
var n = $("#" + this.id.slice(0,1) + "_airline_table tr").length;
$("#" + this.id.slice(0,1) + "_airline_table tr").each(function(){
if ( $("input[type=checkbox]",this).is(':checked') ) {
if ( n > 2 ) {
$(this).remove();
n--;
//alert("Value n-- is: " + n);
} else {
$(this).find("input").val('');
$(this).find("input[type=checkbox]").removeAttr("checked");
$(this).find('option:first').attr('selected', 'selected').parent('select');
}
}
});
});
This jquery does what I want very well. The user can select any particular flight details and delete them, or the user can delete them all.
However, when the user decides to delete them all, the jquery I have will delete the row start from row number 2 (where shown is the figure above is Flight 1). I need to come up with another way where the row MUST be deleted from the last row, which is Flight 15, then Flight 14, Flight 13 and so on. I will keep the row number 2 (used by Flight 1) by making all inputs value empty for this row.
I did try use jquery :gt selectors, it does not work like what I want. So if someone out there can help me to delete the row from the very last row (for delete all) and keep the feature where the user can delete particular flight based on the user selection, I would really appreciate it.
Thank you so much for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情:
你可能需要几个parent()。parent()取决于你的html的样子。
try something like this:
You might need a couple parent().parent() depends on how your html looks like.
抱歉,我在处理您的代码时遇到问题,因此我将以不同的方式构建它。我会尽力对其进行适当的评论,以便它仍然有意义。
Sorry I'm having trouble working around with your code, so I'm just going to build it differently. I'll try to comment it appropriately so that it still makes sense.
我已经找到了解决方案。解决方案实际上并没有回答我的问题。该解决方案是一种解决此问题的方法。
我之所以问如何从最后一行删除表行,是为了保持 id 与数字 1 一致,依此类推。
因为,它只是一个ID,用于唯一标识该字段,因此该值可以进入数据库,我只使用任何数字,只要它是唯一的即可。我编写了 jQuery 脚本来查找现有的表行,并替换与该表行关联的输入之一中非数字的任何内容。
然后,我将检查另一个表行中的数字是否存在。如果存在,我只需将此数字加一。我使用 .each() 来执行此操作,一旦找不到现有数字就会停止。该数字将用于唯一标识该字段。
I have found the solution for this. The solution is actually not answering my question. The solution is kind of a work around for this.
The reason why I asked how to delete the table row from the very last row is to keep the id consistent from number 1 and so on.
Since, it's only an ID, to uniquely identified the field, so the value can go to the database, I just use any number, as long as it's unique. I wrote the jQuery script to find the existing table row and replace anything that is not number from one of the input that is associated to this table row.
Then, I will check whether the number is exist or not from the other table row. If it exist, I just add one the this number. I use .each() to do this, and will stop once it can't find existing number. This number, then will be used to uniquely identified the field.