如何在表中指定的行数之后循环

how to loop after specified number of rows in table

本文关键字:之后 循环      更新时间:2023-09-26

我有一个<table id="table"> .

用户可以在单击"<button id="new">"时在此表中添加行。

用户可以通过单击"<button id="save">

如果用户添加的行超过 5 行而不保存数据,则他们应该会收到要保存的警报。应在每 5 个新行中重复此警报。

我试过这个:

$('#new').click(function(){
  var count = $('#table tbody').children('tr').length;
  if (count > 4) {
  var y = confirm("you should to save your data!");
  if (y == true){
    $('#save').click();
  }
  else {
  }
});
不应使用

> 运算符,而应使用取模运算符。

因此,请将if更改为:

if (count > 0 && count % 5 == 0) {

另一种优雅的方式如下:

$('#new').click(function(){
  var $newRow = $('<tr><td>new</td></tr>').appendTo('table')
  if ($newRow.is('table tr:nth-child(5n)')){
    alert('Alert')
  };
});