0
我有一個如下所述的表單。我無法用每個元素中傳遞的值的簡單JavaScript來計算。需要幫助。當表單元素id和名稱在數組中時的javascript計算
<script>
function calculate(){
c = document.getElementById('count').value;
for(k = 0; k<c; k++){
var amt = 0;
var tamt = 0;
totamt[k] = document.getElementById('Amount').value[k]*document.getElementById('Months').value[k];
document.getElementById('TotAmt').value[k] = totamt[k];
}
}
</script>
<table>
<tr>
<td>SN</td>
<td>Fee Description</td>
<td>Amount</td>
<td>Occurence</td>
<td>Total Amount</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td><select name="FeeCode[]" class="form-control" required>
<option value=""></option>
<?php echo $fClass?>
</select></td>
<td><input type="text" name="Amount[]" id="Amount[]" class="form-control" maxlength="10" style="text-align: right" required></td>
<td><select name="Months[]" id="Months[]" class="form-control" onchange="calculate()" required>
<option value=""></option>
<option value=0>One Time</option>
<option value=1>Monthly</option>
<option value=3>Quarterly</option>
<option value=6>Half Yearly</option>
<option value=12>Annually</option>
</select></td>
<td><input type="text" name="TotAmt[]" id="TotAmt[]" class="form-control" maxlength="10" value="0" style="text-align: right" required></td>
<td><a href="javascript:void(0);" class="addCF">Add More</a></td>
</tr>
<tr>
<td>2</td>
<td><select name="FeeCode[]" class="form-control" required>
<option value=""></option>
<?php echo $fClass?>
</select></td>
<td><input type="text" name="Amount[]" id="Amount[]" class="form-control" maxlength="10" style="text-align: right" required></td>
<td><select name="Months[]" id="Months[]" class="form-control" onchange="calculate()" required>
<option value=""></option>
<option value=0>One Time</option>
<option value=1>Monthly</option>
<option value=3>Quarterly</option>
<option value=6>Half Yearly</option>
<option value=12>Annually</option>
</select></td>
<td><input type="text" name="TotAmt[]" id="TotAmt[]" class="form-control" maxlength="10" value="0" style="text-align: right" required></td>
<td><a href="javascript:void(0);" class="addCF">Add More</a></td>
</tr>
<tr>
<td>3</td>
<td><select name="FeeCode[]" class="form-control" required>
<option value=""></option>
<?php echo $fClass?>
</select></td>
<td><input type="text" name="Amount[]" id="Amount[]" class="form-control" maxlength="10" style="text-align: right" required></td>
<td><select name="Months[]" id="Months[]" class="form-control" onchange="calculate()" required>
<option value=""></option>
<option value=0>One Time</option>
<option value=1>Monthly</option>
<option value=3>Quarterly</option>
<option value=6>Half Yearly</option>
<option value=12>Annually</option>
</select></td>
<td><input type="text" name="TotAmt[]" id="TotAmt[]" class="form-control" maxlength="10" value="0" style="text-align: right" required></td>
<td><a href="javascript:void(0);" class="addCF">Add More</a></td>
</tr>
</table>
<input type="hidden" value="1" name="count" id="count" />
<button type="submit" class="btn btn-success">Submit</button>
對於每一行,id="TotAmt[]"
應有的價值id="Months[]" * id="Amount[]"
。
需要簡單的JavaScript計算相同