简体   繁体   English

使用Jquery基于td值更改行颜色

[英]Change Row Color based on td value using Jquery

I have a table that gets populated from a database. 我有一个从数据库填充的表。 I have 2 conditions that i need to apply 我有2个条件需要申请

  1. Apply Zebra striping to the Table (Completed) 将Zebra条纹应用于表格(已完成)
  2. Change Row color to red based td value 将行颜色更改为基于红色的td值

​` `

<tr class="alt">
    <td class="status"><input type="text" value="One"></td>
    <td class>Received</td>
</tr>
<tr class="alt">
    <td class="status"><input type="text" value="One"></td>
    <td class>Received</td>
</tr>
<tr class="alt">
    <td class="status"><input type="text" value="Zero"></td>
    <td class>Received</td>
</tr>
<tr class="alt">
    <td class="status"><input type="text" value="One"></td>
    <td class>Received</td>
</tr>
<tr class="alt">
    <td class="status"><input type="text" value="Zero"></td>
    <td class>Received</td>
</tr>

​` `

$(document).ready(function()
{
$("tr.alt:even").css("background-color", "#f0f8ff");
$("tr.alt:odd").css("background-color", "#fcfceb");
});

$(document).ready(function() {
   $('.status.val():contains("Zero")').closest('tr.alt').css('background-color', '#cd0000');
});

I wanna change the row color based on the input value 我想根据输入值更改行颜色

<td class="status"><input type="text" value="One"></td>

Here is a fiddle of what I've done so far 这是我到目前为止所做的一个小提琴

Would appreciate the help. 很感激帮助。

Try this, 尝试这个,

Live Demo 现场演示

$('td.status[value=Zero]').css('background-color', 'red');

Edit: Based on comments and change in OP 编辑:根据OP中的注释和更改

To change the whole row with the given criteria of td you can do it this way. row with the given criteria of td更改整row with the given criteria of td您可以这样做。

Live Demo 现场演示

$('td.status[value=Zero]').closest('tr').css('background-color', 'red');

To change the tr (you're using v 1.6.4 instead of latest so we need bind instead of on) 要更改tr(你使用的是v 1.6.4而不是最新的,所以我们需要bind而不是on)

$(document).ready(function(){

    $("tr.alt:even").addClass("even");
    $("tr.alt:odd").addClass("odd");
    $('td.status input').bind('change keyup', function(){
        var tr=$(this).closest('tr');

        if ($(this).val()=='Zero') tr.addClass('zero');       
        else tr.removeClass('zero');

    }).trigger('change'); // the trigger is to run this action on load
});
​
tr.odd
{
    background-color:#fcfceb;
}

tr.even
{
    background-color:#f0f8ff;
}

tr.odd.zero
{
    background-color:#ff0000;
}
tr.even.zero
{
    background-color:#cc0000;
}

Your HTML is a bit messed up though. 你的HTML虽然有点搞砸了。 You have missing quotes and <td class> is invalid. 您缺少引号, <td class>无效。

http://jsfiddle.net/MMEhc/158/ http://jsfiddle.net/MMEhc/158/

EDIT: Updated version to suit the values being changed manually, not just those that are outputted (as I understood the question to be) 编辑:更新版本以适应手动更改的值,而不仅仅是输出的值(我理解的问题是)

http://jsfiddle.net/MMEhc/159/ http://jsfiddle.net/MMEhc/159/

You'll see I moved the background colours out of the HTML and into the CSS to make it easier to manipulate. 您将看到我将背景颜色移出HTML并进入CSS以使其更易于操作。 I also adjusted the red for even or odd rows. 我还调整了偶数行或奇数行的红色。

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

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