表单文本字段上的jQuery计算

时间:2012-04-27 12:11:00

标签: javascript jquery

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

目前有表格,分为两面, 在左边我有一个右边的组合清单是负债 有点帐户的资产负债表,见下图

Balance sheet form

刚才使用的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 每个可用性都有类.liability 每个月付款字段都有类.liabilitymth

我尝试过做jQuery('。asset')。每个(function()并尝试在资产字段中生成总数,对于其他2个部分也是如此,

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

资产:页面左侧的总数将是总资产。 负债:“小计”将反映“责任”栏的总和。 剩余:这将代表资产与资产之间的差异(负面 - 或'正面'+')。负债 - 余额栏。

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

每月 - 剩余/赤字:这应该反映总收入收入的净差额,无论是负面还是正面(从“雇佣”标签中找到的数字),与“每月付款”列的总和相比。

这就是我的jQuery面无表情的地方,必须有一种更简单的方法来计算字段总数,任何人对此有所了解,或者更好地使用代码,而不是将其全部包含在非常大的密钥请求:)

3 个答案:

答案 0 :(得分:2)

这可以让你先行一步:

var totalAss = 0;
jQuery('.asset').each(function(){
    var value = parseFloat(this.value);
    if (value)
        totalAss += value;
});
jQuery('#ass_total').val(totalAss);

答案 1 :(得分: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);

答案 2 :(得分:0)

如果您在keyup或任何其他关键事件中进行计算,则会出现跟踪输入的旧数据的问题。例如,在你输入50然后你把它变成500然后你让它说30时。然后,keyevent将无法找出旧数据和新数据之间的差异。您必须编写一些逻辑来计算旧的和新的更新数据,然后根据您需要更新最终值。如果您可以编写类似的代码,那就更好了......

http://jsfiddle.net/AvSEG/