我目前有以下代碼,可以在每行中選擇總計1和總計2,因爲我選擇了它們各自的表單域。整個表格處於循環狀態。從已填充的單元格中獲取總列數
我被困在如何獲得總共2列?
<script type="text/javascript">
var sumScoreF = function(a) {
var rowtotal = 0;
n = new Array();
for (i = 1; i <= 4; i++) {
if (parseInt(document.getElementById(a + i).value) >= 0) {
n[i] = parseFloat(document.getElementById(a + i).value);
rowtotal += n[i];
}
}
document.getElementById(a + 'total').value = n[2] - n[1];
document.getElementById(a + 'gtotal').value = n[2] - n[1] + n[3] + n[4];
document.getElementById(a + 'ggtotal').value = n[3] + n[4];
};
<form action="test.php" method="post">
<table border="1">
<tr>
<td>Time in</td>
<td>Time out</td>
<td>Total 1</td>
<td>Option 1</td>
<td>Option 2</td>
<td>Total 2</td>
</tr>
<tr>
<?php
for ($j = 1; $j <= 3; $j++)
{
?><td><select name = "time_in" id="<?php echo 'p'.$j.'g1'; ?>" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" />
<option value = 0>---</option>
<option value = 8>08:00</option>
<option value = 8.25>08:15</option>
<option value = 8.5>08:30</option>
</select></td>
<td><select name = "time_out" id="<?php echo 'p'.$j.'g2'; ?>" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" />
<option value = 0>---</option>
<option value = 12>12:00</option>
<option value = 12.25>12:15</option>
<option value = 12.5>12:30</option>
<option value = 12.75>12:45</option>
</select></td>
<td><input name="total_1" id="<?php echo 'p'.$j.'gtotal'; ?>" type="text" /></td>
<td><input type='text' value = "0" name = 'other_1' id="<?php echo 'p'.$j.'g3'; ?>" type="text" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" size = "4" />
<td><input type='text' value = "0" name = 'other_2' id="<?php echo 'p'.$j.'g4'; ?>" type="text" onChange="sumScoreF('<?php echo 'p'.$j.'g'; ?>')" size = "4" />
<td><input type='text' name = 'total_2' size = '4' id="<?php echo 'p'.$j.'ggtotal'; ?>" ></td>
</tr><br>
<?php
}
?>
<td><input type='text' name = 'grand_total'> Grand total = </td>
</table><br>
</form>
爲什麼不使用HTML的'class'用'each'的jQuery? –
我該怎麼做? – Gino
做一個小提琴是最好的,首先把真正的HTML有一些清除,然後我會給你的問題的例子。 –