Skip to main content
 首页 » 编程设计

jquery之有没有一种快速的方法来使用 jQuery 删除表行

2026年03月27日1207java哥

我有一个 50 奇数行 11 列的表。每行都有一个唯一的id由 id="row_<clientid>_rownumber" 组成。第二列中有一个复选框 id="group_<clientid>_<petid>_rownumber"

Redacted Screenshot http://www.forsythesit.com.au/res/img/slowrowremoval.jpg

当用户单击复选框时,我想删除除属于所选客户端的行之外的所有行。我的代码的工作原理如下:

var sClient = $(this).attr("id").substring(6); // trim off group_ 
sClient = sClient.substring(0,sClient.indexOf("_")); // trim off anything after clientid 
$("tr[id^=row_]").not("tr[id^=row_" + sClient + "]").remove();  

问题是它花费的时间太长,以至于在 IE 中我收到“脚本花费时间太长”的警告。

是否有更快的方法来删除多行?

顺便说一句:使用 jQuery 1.4.3 需要 4.4 秒,使用 jQuery 1.4.2 需要 1.3 秒

问题已解决,感谢大家。最终提示由@VisusZhao 提供。这是最终的工作片段:

var KeepRows = $("#bookingstable tbody tr[id^=row_" + sClient + "_]"); 
$("#bookingstable tbody").empty(); 
$("#bookingstable tbody").append(KeepRows); 

谢谢大家

请您参考如下方法:

您可以首先为客户端存储该行,

var clientRow = $('#row_' + sClient); 

然后清空表格

$('#table_id tbody').empty(); 

然后将行插入后面

$('#table_id tbody').append(clientRow); 

这不会发生循环,所以它的时间是恒定的