我想做一个程序,可以动态添加/删除表格的行,
输入数字后可以自动运算求和,添加个求和按钮也行啊,请高人指点,谢谢!

解决方案 »

  1.   

    参照http://www.cnblogs.com/fooo/archive/2009/05/18/1459631.html同时加入事件onchange
    <input type="text" onchange=''/>再调用方法处理合计函数。
      

  2.   

    使用jQuery最为简单
    <table id="table1">
            <thead>
                <tr>
                    <th>成人</th>
                    <th>老人</th>
                    <th>儿童</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="adult"><input type="text" onkeyup="sum('Adult')" /></td>
                    <td class="oldman"><input type="text" onkeyup="sum('Oldman')" /></td>
                    <td class="children"><input type="text" onkeyup="sum('Children')" /></td>
                    <td>
                        <a href="#" onclick="return createRow()">添加</a> | 
                        <a href="#" onclick="return deleteRow(this)">删除</a>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td><input id="sumAdult" type="text"</td>
                    <td><input id="sumOldman" type="text" /></td>
                    <td><input id="sumChildren" type="text" /></td>
                    <td>合计</td>
                </tr>
            </tfoot>
        </table>    function createRow() {
            $('#table1 tbody tr:first').clone(true).appendTo($('#table1 tbody')).find('input[type=text]').val('');
            return false;
        }    function deleteRow(elem) {
            if ($('#table1 tbody tr').length > 1) $(elem).parents('tr').remove();
            sum('Adult');
            sum('Oldman');
            sum('Children');
            return false;
        }    function sum(sumType) {
            var total = 0;
            $('#table1 tbody .' + sumType.toLowerCase() + ' input').each(function() {
                total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
            });
            $('#sum' + sumType).val(total);
        }