0

编辑

现在我有这个html:

<table id="consumo">
            <thead>
                <tr>
                    <th>Semana</th>
                    <th colspan="7">Consumo de alimento diario</th>
                    <th rowspan="2">Total Semana</th>
                    <th rowspan="2">Acumulado</th>
                    <th rowspan="2">Consumo Diario</th>
                    <th rowspan="2">Consumo por pollo semana</th>
                    <th rowspan="2">Consumo Acumulado</th>
                </tr>
                <tr>
                    <td></td>
                    <td>Lunes</td>
                    <td>Martes</td>
                    <td>Miércoles</td>
                    <td>Jueves</td>
                    <td>Viernes</td>
                    <td>Sábado</td>
                    <td>Domingo</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td><input class="con_sem_1" name="con_lun[]" type="text"
                        onkeypress="numeric(event)" size="6" value=""></td>
                    <td><input class="con_sem_1" name="con_mar[]" type="text"
                        onkeypress="numeric(event)" size="6" value=""></td>
                    <td><input class="con_sem_1" name="con_mie[]" type="text"
                        onkeypress="numeric(event)" size="6" value=""></td>
                    <td><input class="con_sem_1" name="con_jue[]" type="text"
                        onkeypress="numeric(event)" size="6" value=""></td>
                    <td><input class="con_sem_1" name="con_vie[]" type="text"
                        onkeypress="numeric(event)" size="6" value=""></td>
                    <td><input class="con_sem_1" name="con_sab[]" type="text"
                        onkeypress="numeric(event)" size="6"></td>
                    <td><input class="con_sem_1" name="con_dom[]" type="text"
                        onkeypress="numeric(event)" size="6" value=""></td>
                    <td><input id="tot_sem_1" disabled="disabled" size="7"></td>
                    <td><input id="acum_sem_1" disabled="disabled" size="7"></td>
                    <td><input id="con_diario_sem_1" disabled="disabled" size="7"></td>
                    <td><input id="con_x_pollo_sem_1" disabled="disabled" size="7"></td>
                    <td><input id="con_acum_sem_1" disabled="disabled" size="7"></td>
                </tr>
            </tbody>
        </table>

这个javascript代码:

function calcular() {
    var total = 0;

    $(".con_sem_1").each(function(){
        if (!isNaN(this.value)) {
            total += parseInt(this.value);
        }
    });

    $("#tot_sem_1").val(total);
}

$(document).ready(function() {
    $("#consumo").on("keyup", ".con_sem_1", calcular());
});

它仍然无法正常工作。

4

1 回答 1

0

我们可以很容易地简化这个 js 代码。只要您将表格放入 html 并为它们设置值,您就可以使用以下内容:

<html>
<head>
<title>

</title>
<script src="http://www.andy-howard.com/js/libs/jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function () {

total = [];

$(".fieldToAdd").each(function ()                                         
    {
    currentNumber = parseInt($(this).val());
    total.push(currentNumber);
    });
    total = eval(total.join('+'));
    alert (total);
});

</script>

</head>
<body>

<table border="0" cellpadding="0" cellspacing="0">
<tr><td><input class="fieldToAdd" type="text" value="1" /></td></tr>
<tr><td><input class="fieldToAdd" type="text" value="2" /></td></tr>
<tr><td><input class="fieldToAdd" type="text" value="3" /></td></tr>
</table>

</body>
</html>
于 2013-07-12T22:25:46.103 回答