开发者

Calculate and display value in the last td

Goal:

Calculate and display right answer in the last td in the tr by selecting row(s). For instance, if I choose the second row, I would get value 44 and it should be applied in the last td of the row.

Problem:

I know how to do it in javascript but in jQuery.

Need help with the source code in jQuery.

// fullmetalboy


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <title>index</title>

    <SCRIPT language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            cell2.innerHTML = rowCount + 1;

            var cell3 = row.insertCell(2);
            cell3.innerHTML = "22";

            var cell4 = row.insertCell(3);
            cell4.innerHTML = "";


        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) 
            {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];

                if(null != chkbox && true == chkbox.checked) 
                {
                    var sdf = 23;


                    //table.deleteRow(i);
           开发者_JAVA技巧         //rowCount--;
                    //i--;
                }

            }
            }catch(e) {
                alert(e);
            }
        }

    </SCRIPT>


    </head>

    <body>

    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

    <INPUT type="button" value="Calculate selected row" onclick="deleteRow('dataTable')" />

    <TABLE id="dataTable" width="350px" border="1">
        <TR>
            <TD><INPUT type="checkbox" name="chk"/></TD>
            <TD>1</TD>
            <TD>22</TD>
            <TD></TD>            
        </TR>
    </TABLE>        



    </body>
</html>


You could do (you can check more than one columns and the result is calculated for each row):

$(document).ready(function(){
    $('#calculate').click(function(){
        $('input:checkbox:checked').each(function(){
          var first = parseInt($(this).closest('td').next('td').text(), 10);

         var sec = parseInt($(this).closest('td').next('td').next('td').text(), 10);
            var result = (first*sec);
            $(this).closest('tr').find('td:last').html(result)

        });
    });
});

Fidlle here: http://jsfiddle.net/D2uVz/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜