这是更长形式的一小部分(为了清晰起见,添加了粉红色,绿色和黄色框/箭头):
原始
标记:
基本上 - 将数字输入季度 - >检查(粉红色框),值自动求和并自动填充只读字段“总现金”(这已经在工作)。然后,一旦“退款付款”字段输入了一个值(包括零),它就会自动与“总现金”相加并填充“最终总计”字段 - 这对于每一列都是独立的。这有效 - 但我希望我不必强迫用户在退款付费中输入值,除非有值。最后 - 我还需要从“存款”和“经理站”栏目中的“最终总计”到自动汇总到“总存款”这似乎根本不起作用......
最后 - 我试图实现一个按钮来重新计算所有总数,然后再单击“提交”将其转换为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);
}
答案 0 :(得分:1)
有效 - 但我希望我不必强迫用户输入值 在退款中支付,除非有价值。
这就是你想要的吗? 试试下面的代码
1 /添加此功能
function showAllTotal(){
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);
}
2 /修改某处:将上述功能添加到每个事件
$(".dep").each(function() {
$(this).keyup(function(){
calculateSumForInput(".dep", "#depTotal");
showAllTotal();
});
});
$(".man").each(function() {
$(this).keyup(function(){
calculateSumForInput(".man", "#manTotal");
showAllTotal();
});
});
$(".safe").each(function() {
$(this).keyup(function(){
calculateSumForInput(".safe", "#safeTotal");
showAllTotal();
});
});
3 /替换“$(”。depFinalFactor“)中的函数.keyup(function()”with below
$(".depFinalFactor").keyup(function() {
showAllTotal();
});
我所做的是将总值总结的函数移动到函数中,并且每次用户开始在表中的一个字段上输入数字时重复使用它。一切都在您的代码中,仅此而已!
编辑:我也在小提琴上更新了你的代码。