-1

I want to get the average and max of the <td> values when a user clicks calculate button to the labels beside average and max

<html>
<table id="table" style="height:350px;margin-left:1em;width:700px;">
    <!--this is my table header-->
    <tr style="display:table-row">
        <th class="checkbox"><input type="checkbox"/></th>
        <th class="Name">NAME</th>
        <th class="Score">SCORE</th>
        <th class="Email">EMAIL</th>
        <th class="Empty"></th>
    </tr>
    <tr>
        <!--tabledata-->
        <td ><input type="checkbox" /></td>
        <td >Vijay Prakash</td>
        <td >34</td>
        <td >vijay@example.com</td>
        <td ></td>
    </tr>
    <tr>
        //table data
        <td ><input type="checkbox" /></td>
        <td >Sashi Pagadala</td>
        <td >21</td>
        <td >sashi@example.com</td>
        <td ></td>
    </tr>
</table>
<input type="button" id="btnCalculate" value="Calculate"/>
<label>Average:</label>
<label id="lblAverage"></label>
<label>Max:</label>
<label id="lblMax"></label>
</form>
</html>
4

1 に答える 1

1

これを簡単にするために、番号を含む TD にクラスを追加しました。

HTML:

<td ><input type="checkbox" /></td>
<td >Vijay Prakash</td>
<td class="number">34</td>
<td >vijay@gmail.com</td>
<td ></td>

jQuery

$("#btnCalculate").click(function() {
    var total = 0;
    var highCount = 0;

    $("table tr td.number").each(function() {
        total += Number($(this).text());
        if (highCount < $(this).text()) {
            highCount = $(this).text();
        }
    });

    $("#lblMax").text(highCount);
    $("#lblAverage").text(total / $("table tr td.number").length);
});

フィドル: http://jsfiddle.net/Kjm6Z/1/

于 2013-05-01T13:21:06.343 に答える