简体   繁体   English

更改表格中特定索引的颜色

[英]Changing colour of a particular index in a table

A table has two rows and three columns. 一个表有两行三列。 The requirement is that if the sum of second and third column for a particular row is not equal to 12 than the background colou for same row but first column will change to red. 要求是,如果特定行的第二和第三列之和不等于12,而不是同一行的背景色,但第一列将变为红色。

Note: First column is having a text only. 注意:第一列仅包含文本。 Second and third columnns have textboxes containing integer values. 第二和第三列具有包含整数值的文本框。

Example: 例:

<table>
    <tr>
      <td>A</td>
      <td>2</td>
      <td>13</td>
    </tr>
    <tr>
      <td>B</td>
      <td>2</td>
      <td>10</td>
    </tr>
    <tr>
      <td>C</td>
      <td>2</td>
      <td>14</td>
    </tr>
</table>

In the above example, the sum 2+13 and 2+14 are greater than 12, so the background colour of first for both of them should change to red colour. 在上面的示例中,总和2 + 13和2 + 14大于12,因此它们两个的first的背景色都应更改为红色。

You can simply do: 您可以简单地执行以下操作:

$('tr').each(function(i, row) {
    // init sum for each new parsed row
    var sum = 0;
    // parse row children ('td')
    $(this).children('td').each(function(j, col) {
        if (j > 0) {
            // sum all columns, except first
            sum += parseInt($(col).text());
        }
    });
    // check sum
    if (sum != 12) {
        // Change background here for first column of current row
        $(row).find('td').first().css({
            backgroundColor: 'red'
        });
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM