Javascript页面加载总计

Javascript Page Load Total

本文关键字:加载 Javascript      更新时间:2023-09-26

我创建了一个用于计算每月费用的表格。我遇到的问题是在最后一页上,我从以前的页面(会话到数据)收集信息,这些信息会自动填充最后一页的字段。我创建了一个Javascript,假设它减去页面上的五个字段,得到一个总数,但这不起作用。如果我从加载部分删除到数据的会话,Javascript就可以完美地工作。

有问题的页面:http://www.garranteedsolutions.com/budget?chronoform=BudgetPage7

Javascript:

 window.addEvent('domready', function() {
 $('spendable').addEvent('change', rekenen1);
 $('housetotal').addEvent('change', rekenen1);
 $('cartotal').addEvent('change', rekenen1);
 $('creditortotal').addEvent('change', rekenen1);
 $('misctotal').addEvent('change', rekenen1);
});
function rekenen1(){
$('grandtotal').value = Number($('spendable').value) + Number($('housetotal').value) +   Number($('cartotal').value) + Number($('creditortotal').value) + Number($('misctotal').value) ;
}

这是我一直在使用的代码,但它需要在表单框中进行更改才能执行操作。我试过这个

Javascript:

window.addEvent('domready', function() {
rekenen1;
 $('spendable').addEvent(rekenen1);
 $('housetotal').addEvent(rekenen1);
 $('cartotal').addEvent(rekenen1);
 $('creditortotal').addEvent(rekenen1);
 $('misctotal').addEvent(rekenen1);
 });
 function rekenen1(){
 $('grandtotal').value = 
Number($('spendable').value) + Number($('housetotal').value) 
+ Number($('cartotal').value) + Number($('creditortotal').value) 
+ Number($('misctotal').value);
}

这是我从这里开始寻求帮助的延续:http://www.chronoengine.com/forums/viewtopic.php?f=2&t=67427&p=269741#p269741

我对Javascript不是很了解,我很快就要完成这份表格了。我就是无法统计总金额。

问题是,只有当您更改表单的值时,事件才会触发。表单是只读的,因此无法更改。

无论如何,这里是优化的代码:

window.addEvent('domready', function() {
    var rekenen1 = function(){
        var grandTotal = 0;
        $$('#spendable, #housetotal, #cartotal, #creditortotal, #misctotal').each(function(el){
            if(el.value.length > 0) grandTotal += el.value.toFloat();
        });
        $('grandtotal').value = grandTotal;
    };
    $$('#spendable, #housetotal, #cartotal, #creditortotal, #misctotal').addEvent('onchange', refenen1);
});

这应该行得通。该函数检查字段上是否有值,如果有,则将其包含在计算中。

您编写的第二个代码会引发错误,因为您在addEvent函数中缺少一个参数。

我希望这能帮助你:)

这似乎奏效了!所以现在我正试图弄清楚如何获得数千的逗号。所以如果我输入1200,它会显示1200。

window.addEvent('domready', function() {
$('grandtotal').value = Number($('spendable').value) + Number($('housetotal').value) + Number($('cartotal').value) + Number($('creditortotal').value) + Number($('misctotal').value);
});

非常感谢你的帮助!