这是更长表格的一小部分(为清楚起见,添加了粉色、绿色和黄色框/箭头):
原来的:
标记:
基本上 - 当数字输入季度 - >检查(粉红色框)时,值自动求和并自动填充只读字段“总现金”(这已经在工作)。然后,一旦“已支付的退款”字段输入了一个值(包括零),它会自动与“总现金”相加并填充“最终总计”字段 - 这对于每一列都是独立的。那行得通——但我希望我不必强迫用户在已支付的退款中输入一个值,除非有一个值。最后 - 我还需要“存款”和“经理站”列中的“最终总计”来自动汇总到“存款总额”这似乎根本不起作用......
最后 - 我试图实现一个按钮来重新计算所有总数,然后再单击“提交”将其转换为 PDF - 我也被难住了。具有名称和 ID calc
- 我愿意接受任何最佳实践解决方案。我非常乐意进行大量修改 - 作为一个新手,其中很多都采用“我的裤子座位”编码。
我对 Javascript 和 JQuery 非常陌生 - 所以请温柔一点?下面的代码(与此问题相关的部分,无论如何):
HTML
<tr>
<td class="label" colspan="4">Deposit / Safe / Manager Station Count</td>
</tr>
<tr>
<td class="null"></td>
<td class="label">Deposit</td>
<td class="label">Manager Station</td>
<td class="label">Safe</td>
</tr>
<tr>
<td class="label">Quarters</td>
<td class="field"><input type="text" name="depQuarters" class="dep" tabindex="31" /></td>
<td class="field"><input type="text" name="manQuarters" class="man" tabindex="41" /></td>
<td class="field"><input type="text" name="safeQuarters" class="safe" tabindex="51" /></td>
</tr>
<tr>
<td class="label">Coins</td>
<td class="field"><input type="text" name="depCoins" class="dep" tabindex="32" /></td>
<td class="field"><input type="text" name="manCoins" class="man" tabindex="42" /></td>
<td class="field"><input type="text" name="safeCoins" class="safe" tabindex="52" /></td>
</tr>
<tr>
<td class="label">$1 Bills</td>
<td class="field"><input type="text" name="dep1Bills" class="dep" tabindex="33" /></td>
<td class="field"><input type="text" name="man1Bills" class="man" tabindex="43" /></td>
<td class="field"><input type="text" name="safe1Bills" class="safe" tabindex="53" /></td>
</tr>
<tr>
<td class="label">$5 Bills</td>
<td class="field"><input type="text" name="dep5Bills" class="dep" tabindex="34" /></td>
<td class="field"><input type="text" name="man5Bills" class="man" tabindex="44" /></td>
<td class="field"><input type="text" name="safe5Bills" class="safe" tabindex="54" /></td>
</tr>
<tr>
<td class="label">$10 Bills</td>
<td class="field"><input type="text" name="dep10Bills" class="dep" tabindex="35" /></td>
<td class="field"><input type="text" name="man10Bills" class="man" tabindex="45" /></td>
<td class="field"><input type="text" name="safe10Bills" class="safe" tabindex="55" /></td>
</tr>
<tr>
<td class="label">$20 Bills</td>
<td class="field"><input type="text" name="dep20Bills" class="dep" tabindex="36" /></td>
<td class="field"><input type="text" name="man20Bills" class="man" tabindex="46" /></td>
<td class="field"><input type="text" name="safe20Bills" class="safe" tabindex="56" /></td>
</tr>
<tr>
<td class="label">$50 Bills</td>
<td class="field"><input type="text" name="dep50Bills" class="dep" tabindex="37" /></td>
<td class="field"><input type="text" name="man50Bills" class="man" tabindex="47" /></td>
<td class="field"><input type="text" name="safe50Bills" class="safe" tabindex="57" /></td>
</tr>
<tr>
<td class="label">$100 Bills</td>
<td class="field"><input type="text" name="dep100Bills" class="dep" tabindex="38" /></td>
<td class="field"><input type="text" name="man100Bills" class="man" tabindex="48" /></td>
<td class="field"><input type="text" name="safe100Bills" class="safe" tabindex="58" /></td>
</tr>
<tr>
<td class="label">Checks</td>
<td class="field"><input type="text" name="depChecks" class="dep" tabindex="39" /></td>
<td class="field"><input type="text" name="manChecks" class="man" tabindex="49" /></td>
<td class="field"><input type="text" name="safeChecks" class="safe" tabindex="59" /></td>
</tr>
<tr>
<td class="label">Total Cash</td>
<td class="field"><input type="text" name="depTotal" id="depTotal" class="depFinalFactor" readonly /></td>
<td class="field"><input type="text" id="manTotal" name="manTotal" class="manFinalFactor" readonly /></td>
<td class="field"><input type="text" id="safeTotal" name="safeTotal" class="safeFinalFactor" readonly /></td>
</tr>
<tr>
<td class="label">Refunds Paid In</td>
<td class="field"><input type="text" name="depRefunds" id="depRef" class="depFinalFactor" tabindex="40" /></td>
<td class="field"><input type="text" name="manRefunds" tabindex="50" class="manFinalFactor" /></td>
<td class="field"><input type="text" name="safeRefunds" tabindex="60" class="safeFinalFactor" /></td>
</tr>
<tr>
<td class="label">Final Total</td>
<td class="field"><input type="text" name="depFinalTotal" id="depFinalTotal" readonly /></td>
<td class="field"><input type="text" name="manFinalTotal" id="manFinalTotal" readonly /></td>
<td class="field"><input type="text" name="safeFinalTotal" id="safeFinalTotal" readonly /></td>
</tr>
<tr>
<td class="null" colspan="2"></td>
<td class="label">Total to Deposit</td>
<td class="field" id="totalToDeposit"></td>
</tr>
</table>
<input type="button" name="calc" id="calc" value="Recalculate Totals" />
<input style="float: right;" type="submit" name="btn_submit" />
</form>
</body>
</html>
Javascript/JQuery
$(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".accXPT1").each(function() {
$(this).keyup(function(){
calculateSumForInput(".accXPT1", "#accXPT1Total");
});
});
$(".accXPT2").each(function() {
$(this).keyup(function(){
calculateSumForInput(".accXPT2", "#accXPT2Total");
});
});
$(".accXPT3").each(function() {
$(this).keyup(function(){
calculateSumForInput(".accXPT3", "#accXPT3Total");
});
});
$(".dispXPT1").each(function() {
$(this).keyup(function(){
calculateSumForInput(".dispXPT1", "#dispXPT1Total");
});
});
$(".dispXPT2").each(function() {
$(this).keyup(function(){
calculateSumForInput(".dispXPT2", "#dispXPT2Total");
});
});
$(".dispXPT3").each(function() {
$(this).keyup(function(){
calculateSumForInput(".dispXPT3", "#dispXPT3Total");
});
});
$(".dep").each(function() {
$(this).keyup(function(){
calculateSumForInput(".dep", "#depTotal");
});
});
$(".man").each(function() {
$(this).keyup(function(){
calculateSumForInput(".man", "#manTotal");
});
});
$(".safe").each(function() {
$(this).keyup(function(){
calculateSumForInput(".safe", "#safeTotal");
});
});
$(".depFinalFactor").keyup(function() {
var sum = 0;
$(".depFinalFactor").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
mySum = sum.toFixed(2);
if (mySum == $("#moneyAccountedFor").val()) {
$("#totalToDeposit").css("color", "green");
} else {
$("#totalToDeposit").css("color", "red");
}
$("#depFinalTotal").val(mySum);
$("#totalToDeposit").text(mySum);
});
$("#calc").on("click", function() {
var sum = 0;
$(".depFinalFactor").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
mySum = sum.toFixed(2);
if (mySum == $("#moneyAccountedFor").val()) {
$("#totalToDeposit").css("color", "green");
} else {
$("#totalToDeposit").css("color", "red");
}
$("#depFinalTotal").val(mySum);
$("#totalToDeposit").text(mySum);
});
$(".manFinalFactor").keyup(function() {
var sum = 0;
$(".manFinalFactor").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
mySum = sum.toFixed(2);
$("#manFinalTotal").val(mySum);
});
$(".safeFinalFactor").keyup(function() {
var sum = 0;
$(".safeFinalFactor").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
mySum = sum.toFixed(2);
$("#safeFinalTotal").val(mySum);
});
});
function calculateSum(myClass, myID) {
var sum = 0;
//iterate through each textboxes and add the values
$(myClass).each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
myTotal = sum.toFixed(2);
$(myID).text(myTotal);
}
function calculateSumForInput(myClass, myID) {
var sum = 0;
//iterate through each textboxes and add the values
$(myClass).each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
myTotal = sum.toFixed(2);
$(myID).val(myTotal);
}