HTML代码:


<table border="1" cellspacing="0">
    <thead>
        <th></th>
        <th>语文</th>
        <th>数学</th>
        <th>英语</th>
        <th>总分</th>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td><input type="text" placeholder="--" onkeyup="total()"></td>
            <td><input type="text" placeholder="--" onkeyup="total()"></td>
            <td><input type="text" placeholder="--" onkeyup="total()"></td>
            <td id="row1"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" placeholder="--" onkeyup="total()"></td>
            <td><input type="text" placeholder="--" onkeyup="total()"></td>
            <td><input type="text" placeholder="--" onkeyup="total()"></td>
            <td></td>
        </tr>
        <tr>
            <td>合计</td>
            <td id="col1"></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>

JQuery代码:


function total(){
    var colTotal = 0;
    var rowTotal = 0;
    //  计算列和
    $('table tr:gt(0)').each(function(){
        if(parseFloat($(this).find('td:eq(1) input[type="text"]').val())){
            colTotal += parseFloat($(this).find('td:eq(1) input[type="text"]').val());
        }
    });
    $('#col1').text(colTotal);
    //  计算行和
    $('table tr:eq(1) td input[type="text"]').each(function(){
        if(parseFloat($(this).val())){
            rowTotal += parseFloat($(this).val());
        }
    });
    $('#row1').text(rowTotal);
}