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 に答える