3

为我的 jQuery 寻找一些帮助,(我知道它不是最好的)

目前有一个表格,分为两部分,左边我有一个资产清单,右边是负债,有点像账户的资产负债表,见下图

资产负债表形式

刚才使用的 jquery 有点乱..(对不起)这段代码将计算每个 keyup 请求的字段总数,这似乎工作正常,

jQuery('#ass_cash, #ass_liquid, #ass_lifeinsu, #ass_covvalue, #ass_la1, #ass_la2, #ass_la3, #ass_realestate, #ass_auto1total, #ass_auto2total, #lib_mortgage, #lib_bankloan1, #lib_bankloan2, #lib_loansinsucomp, #lib_loanscreditunion, #lib_creditcards, #lib_od1, #lib_od2, #lib_od3, #lib_rent, #lib_mortgagemthpmt, #lib_bankloan1mthpmt, #lib_bankloan2mthpmt, #lib_loansinsucompmthpmt, #lib_loanscreditunionmthpmt, #lib_creditcardsmthpmt, #lib_od1mthpmt, #lib_od2mthpmt, #lib_od3mthpmt, #lib_rentmthpmt').keyup( function(){

// ASSESTS
var ass_cash = jQuery("#ass_cash").val();
var ass_liquid = jQuery("#ass_liquid").val();
var ass_lifeinsu = jQuery("#ass_lifeinsu").val();
var ass_covvalue = jQuery("#ass_covvalue").val();
var ass_la1 = jQuery("#ass_la1").val();
var ass_la2 = jQuery("#ass_la2").val();
var ass_la3 = jQuery("#ass_la3").val();
var ass_realestate = jQuery("#ass_realestate").val();
var ass_auto1total = jQuery("#ass_auto1total").val();
var ass_auto2total = jQuery("#ass_auto2total").val();
var ass_total = jQuery("#ass_total").val();

if(ass_cash==''){ ass_cash = 0; }
if(ass_liquid==''){ ass_liquid = 0; }
if(ass_lifeinsu==''){ ass_lifeinsu = 0; }
if(ass_covvalue==''){ ass_covvalue = 0; }
if(ass_la1==''){ ass_la1 = 0; }
if(ass_la2==''){ ass_la2 = 0; }
if(ass_la3==''){ ass_la3 = 0; }
if(ass_realestate==''){ ass_realestate = 0; }
if(ass_auto1total==''){ ass_auto1total = 0; }
if(ass_auto2total==''){ ass_auto2total = 0; }

var asssubtotal = parseInt(ass_cash) + parseInt(ass_liquid) + parseInt(ass_lifeinsu) + parseInt(ass_covvalue);

asssubtotal = asssubtotal + parseInt(ass_la1) + parseInt(ass_la2) + parseInt(ass_la3) + parseInt(ass_realestate);

asssubtotal = asssubtotal + parseInt(ass_auto1total) + parseInt(ass_auto2total);

var asstotal = jQuery('#ass_total');
asstotal.val(asssubtotal);

// LIABILITIES
var lib_mortgage = jQuery("#lib_mortgage").val();
var lib_bankloan1 = jQuery("#lib_bankloan1").val();
var lib_bankloan2 = jQuery("#lib_bankloan2").val();
var lib_loansinsucomp = jQuery("#lib_loansinsucomp").val();
var lib_loanscreditunion = jQuery("#lib_loanscreditunion").val();
var lib_creditcards = jQuery("#lib_creditcards").val();
var lib_od1 = jQuery("#lib_od1").val();
var lib_od2 = jQuery("#lib_od2").val();
var lib_od3 = jQuery("#lib_od3").val();
var lib_rent = jQuery("#lib_rent").val();

if(lib_mortgage==''){ lib_mortgage = 0; }
if(lib_bankloan1==''){ lib_bankloan1 = 0; }
if(lib_bankloan2==''){ lib_bankloan2 = 0; }
if(lib_loansinsucomp==''){ lib_loansinsucomp = 0; }
if(lib_loanscreditunion==''){ lib_loanscreditunion = 0; }
if(lib_creditcards==''){ lib_creditcards = 0; }
if(lib_od1==''){ lib_od1 = 0; }
if(lib_od2==''){ lib_od2 = 0; }
if(lib_od3==''){ lib_od3 = 0; }
if(lib_rent==''){ lib_rent = 0; }

var libsubtotal = parseInt(lib_mortgage) + parseInt(lib_bankloan1) + parseInt(lib_bankloan2) + parseInt(lib_loansinsucomp);

libsubtotal = libsubtotal + parseInt(lib_loanscreditunion) + parseInt(lib_creditcards) + parseInt(lib_od1) + parseInt(lib_od2);

libsubtotal = libsubtotal + parseInt(lib_od3) + parseInt(lib_rent);

var lib_subtotal = jQuery('#lib_subtotal');                 lib_subtotal.val(libsubtotal);                      

// MONTHLY PAYMENTS
var lib_mortgagemthpmt = jQuery("#lib_mortgagemthpmt").val();
var lib_bankloan1mthpmt = jQuery("#lib_bankloan1mthpmt").val();
var lib_bankloan2mthpmt = jQuery("#lib_bankloan2mthpmt").val();
var lib_loansinsucompmthpmt = jQuery("#lib_loansinsucompmthpmt").val();
var lib_loanscreditunionmthpmt = jQuery("#lib_loanscreditunionmthpmt").val();
var lib_creditcardsmthpmt = jQuery("#lib_creditcardsmthpmt").val();
var lib_od1mthpmt = jQuery("#lib_od1mthpmt").val();
var lib_od2mthpmt = jQuery("#lib_od2mthpmt").val();
var lib_od3mthpmt = jQuery("#lib_od3mthpmt").val();
var lib_rentmthpmt = jQuery("#lib_rentmthpmt").val();

if(lib_mortgagemthpmt==''){ lib_mortgagemthpmt = 0; }
if(lib_bankloan1mthpmt==''){ lib_bankloan1mthpmt = 0; }
if(lib_bankloan2mthpmt==''){ lib_bankloan2mthpmt = 0; }
if(lib_loansinsucompmthpmt==''){ lib_loansinsucompmthpmt = 0; }
if(lib_loanscreditunionmthpmt==''){ lib_loanscreditunionmthpmt = 0; }
if(lib_creditcardsmthpmt==''){ lib_creditcardsmthpmt = 0; }
if(lib_od1mthpmt==''){ lib_od1mthpmt = 0; }
if(lib_od2mthpmt==''){ lib_od2mthpmt = 0; }
if(lib_od3mthpmt==''){ lib_od3mthpmt = 0; }
if(lib_rentmthpmt==''){ lib_rentmthpmt = 0; }

var lib_surplus = jQuery('#lib_surplus');
if(lib_surplus==''){ lib_surplus = 0; }

var subtotal = parseInt(lib_mortgagemthpmt) + parseInt(lib_bankloan1mthpmt) + parseInt(lib_bankloan2mthpmt) + parseInt(lib_loansinsucompmthpmt);

subtotal = subtotal + parseInt(lib_loanscreditunionmthpmt) + parseInt(lib_creditcardsmthpmt) + parseInt(lib_od1mthpmt) + parseInt(lib_od2mthpmt);

subtotal = subtotal + parseInt(lib_od3mthpmt) + parseInt(lib_rentmthpmt);

var totalmthpmt = jQuery('#lib_totalmthpmt');
totalmthpmt.val(subtotal);

var assets_total = jQuery('#ass_total').val();
var lib_subtotal  = jQuery('#lib_subtotal').val();
var lib_surplus  = jQuery('#lib_surplus');

if(assets_total==''){ assets_total = 0; }
if(lib_subtotal==''){ lib_subtotal = 0; }
if(lib_surplus==''){ lib_surplus = 0; }

var surplus = assets_total - lib_subtotal;
lib_surplus.val(surplus);

// THIS IS THE PART THAT ISNT WORKING               
//surplus/deficit
//var lib_total = jQuery('#lib_total').val();
//if(lib_total==''){ lib_total = 0; }
//var lib_totalmthpmt = jQuery('#lib_totalmthpmt').val();
//if(lib_totalmthpmt==''){ lib_totalmthpmt = 0; }

//var surplustotal = lib_total - lib_totalmthpmt;
//jQuery('#mthsurplus').val(surplustotal);
});

您可以看到上面导致问题的部分,它是小计(减去)盈余之间的计算,以生成总数,

资产的每个字段都有一个类 .asset 每个 libability 都有一个类 .liability 每个月支付字段都有一个类 .liabilitymth

我试过做 jQuery('.asset').each(function() 并尝试在资产字段中生成总数,其他 2 个部分相同,

灰色框是“只读的”,应该出现计算。

资产:页面左侧的总和将是总资产。负债:“小计”将反映负债列的总和。SURPLUS:这将表示资产和负债-余额列之间的差异(负'-'或正'+')。

总计(右侧):这是为了创建“资产负债表”效果,因此当您查看它时,计算应反映与资产侧(左侧)的“总计”相同的数字。

每月盈余/赤字:这应反映总收入收入与“每月支付”栏的总额相比的净差异,无论是负数还是正数(从就业选项卡中找到的数字)。

这就是我的 jQuery 面面俱到的地方,必须有一种更简单的方法来计算字段总数,任何人都对此有所了解,或者更好地使用代码,而不是将其全部包装在一个非常大的 keyup 下要求 :)

4

3 回答 3

2

这可以为您提供先机:

var totalAss = 0;
jQuery('.asset').each(function(){
    var value = parseFloat(this.value);
    if (value)
        totalAss += value;
});
jQuery('#ass_total').val(totalAss);
于 2012-04-27T12:16:17.990 回答
0

如果您在 keyup 或任何其他关键事件中进行计算,那么将存在跟踪输入的旧数据的问题。例如,在某个瞬间,您输入了 50,然后您输入到 500,然后您输入了 30。然后,keyevent 将无法找出新旧数据之间的差异。您将必须编写一些逻辑来计算新旧更新的数据,然后根据这些逻辑您将不得不更新最终值。如果你能编写这样的代码会更好......

http://jsfiddle.net/AvSEG/

于 2012-04-27T14:12:54.003 回答
0

确实有一些简单的方法。做这样的事情

在所有类上意味着 .assets 、 .liability 和 .liabilitymth 在 document.ready 上执行此操作

$(function()
{
$('.assets , .liability ,.liabilitymth').each(function()
{
        var value = $(this).val();
        if(value == "")
        {
            $(this).val('000.00');
        }   
});

});

现在

var total_assets = 0;
$('.assets').keyup(function(){
      var number = $(this).val();
       total_assets += parseInt(number);
});

在那之后

$('#total_assets').val(total_assets);
于 2012-04-27T12:27:43.793 回答