修改
现在我有了这个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());
});
它仍然不起作用。
答案 0 :(得分: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>