我试图删除一个表中的行是空的,但不是在完整的表只有从"tr ("+x+"+5)"从"td("+我+,+ 7)input"。
我试图从输入的行中求和值,如果结果是"0", "null"或";undefined"。但是我不能让它工作。
表现在有150 tr和15 td,但这应该动态设置。最大值从表底第5位起为"tr"右边的最大值是"td"从7号开始。
$(".produkttabelle_alt tr:nth-child(n+5)").each(function(i, obj){
var sum = 0;
$(this).children("td:nth-child(n+7)").children().each(function(){
var input_val_sum = $(this).val();
sum += parseInt(input_val_sum);
});
if ((sum != null) || (sum != "") || (sum != "undefined") || (sum != 0) ) {
$(this).closest(".old_row_tr").hide();
}});
考虑以下内容:
$(".produkttabelle_alt tr:nth-child(5n)").each(function(i, row) {
var sum = 0;
$("td:nth-child(7n)", row).each(function(j, cell) {
sum += parseInt($("input", cell).val());
});
if ((sum != null) || (sum != "") || (sum != "undefined") || (sum != 0)) {
console.log("Hide Row " + ($(row).index() + 1));
$(row).hide();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="produkttabelle_alt">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
</table>
请参阅:https://api.jquery.com/nth-child-selector/
由于jQuery对
:nth-
选择器的实现严格来源于CSS规范,因此n
的值为"1-indexed",这意味着计数从1
开始。
如果您想要每隔5行瞄准一次,则使用5n
。如果你想要每隔7个单元格,使用7n
。
输入的值总是字符串,所以不可能是null
或undefined
。它可能是字母或其他字符。所以你可以考虑使用isNaN()
函数。
if(isNaN(sum) || sum == 0){
row.hide();
}
这将捕获空或0值行。下面是一个例子:
$(".produkttabelle_alt tr:nth-child(5n)").each(function(i, row) {
var sum = 0;
$("td:nth-child(7n)", row).each(function(j, cell) {
sum += parseInt($("input", cell).val());
});
if (isNaN(sum) || (sum == 0)) {
console.log("Hide Row " + ($(row).index() + 1));
$(row).hide();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="produkttabelle_alt">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<!-- Test Row -->
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="0" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="0" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="0" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
<!-- Test Row -->
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="" /></td>
<td>22</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td><input type="text" value="7" /></td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td><input type="text" value="14" /></td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td><input type="text" value="21" /></td>
<td>22</td>
</tr>
</table>
谢谢你@Twisty,我可以解决这个问题。非常感谢!这个代码片段现在用于向空行或子行7n之和为0的行添加类。
$(".produkttabelle_alt tr:nth-child(n+5)").each(function(i, obj){
var sum = 0;
$(this).children("td:nth-child(n+7)").children().each(function(){
var input_val_sum = $(this).val();
sum += parseInt(input_val_sum);
});
if (isNaN(sum) || (sum == 0)) {
$(this).closest(".old_row_tr").addClass("not_ordered");
}
});