JQuery select all deselect click change background color

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

I put the CSS style put out, but this can be ignored

body{margin:0;padding:0;font-size:12px;font-family:Microsoft Accor;}
.datagrid{width:100%;}
.datagird tr th{background-color:#191970; font-size:14px;}
.datagrid tr th, .datagrid tr td{border:1px solid #ccc; border-collapse:collapse;}

/* The selected line style */
.table-row-selected{background:#fff68f;}

Let us look at page HTML structure

<div id="page">
    <table class="datagrid" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th><input id="CheckAll" name="checkall" type="checkbox" /></th>
                <th>ID</th>
                <th>Title</th>
                <th>The publisher</th>
                <th>Release time</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td align="center"><input type="checkbox" name="check" /></td>
                <td align="center" width="5%">1</td>
                <td>Ali do what three things? </td>
                <td align="center" width="10%">internet</td>
                <td align="center" width="15%">2013-07-01</td>
            </tr>
            <tr>
                <td align="center"><input type="checkbox" name="check" /></td>
                <td align="center" width="5%">2</td>
                <td>Summary: 16 traditional industries by Internet rewriting</td>
                <td align="center" width="10%">internet</td>
                <td align="center" width="15%">2013-07-01</td>
            </tr>
            <tr>
                <td align="center"><input type="checkbox" name="check" /></td>
                <td align="center" width="5%">3</td>
                <td>If the intelligent mobile phone market has changed, how to do cool people? </td>
                <td align="center" width="10%">internet</td>
                <td align="center" width="15%">2013-07-01</td>
            </tr>
            <tr>
                <td align="center"><input type="checkbox" name="check" /></td>
                <td align="center" width="5%">4</td>
                <td>Have a look at Ford is how to resist Google apple? </td>
                <td align="center" width="10%">internet</td>
                <td align="center" width="15%">2013-07-01</td>
            </tr>
        </tbody>
    </table>
</div>

To realize the function

1)Click to change the background color

2)Select / anti select function

//Click the row change the background color of the selected row
$(".datagrid tbody tr").bind("click", function () {
    var oThis = $(this);
    if (oThis.hasClass("table-row-selected")) {
        oThis.removeClass("table-row-selected");
        oThis.children("td:eq(0)").children("input[name='check']").removeAttr("checked");
    } else {
        oThis.addClass("table-row-selected");
        oThis.children("td:eq(0)").children("input[name='check']").attr("checked", "checked");
    }

    var len = $("table.datagrid tbody").find("input[name='check']").length;
    var count = 0;
    $("table.datagrid").find("input[name='check']").each(function (i) {
        var That = $(this);
        if (That.prop("checked") == true) {
            count++;
        }
    });

    if (len == count) {
        $("input[name='checkall']").attr("checked", "checked");
    } else {
        $("input[name='checkall']").removeAttr("checked");
    }
});

//Select all
$("#CheckAll").bind("click", function () {
    var oThis = $(this);
    if (oThis.prop("checked") == true) {
        $("table.datagrid tbody").find("input[name='check']").attr("checked", "checked");
        $("table.datagrid tbody").find("input[name='check']").parents("tr").addClass("table-row-selected");
    } else {
        $("table.datagrid tbody").find("input[name='check']").removeAttr("checked");
        $("table.datagrid tbody").find("input[name='check']").parents("tr").removeClass("table-row-selected");
    }
});

PS: Functions are too simple, I feel shy to send out. But the work is often write some repetitive code, think or posted

By the way can also increase a little of his own initiative. Thx, that's all


Postscript: the function of all completed, has been re edited out.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Bruce at November 19, 2013 - 11:26 AM