2012-03-18 86 views
0

我創建了一個用於計算每月費用的表單。我遇到的問題是我收集前一頁(會話到數據)的信息的最後一頁,該信息自動填充最後一頁上的字段。我創建了一個JavaScript,假設總頁數減去五個字段,但這不起作用。如果我將會話從加載部分刪除數據,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非常好,我很接近完成此表格。我只是無法讓Grand Total合計。

回答

0

這似乎工作!所以現在我想弄清楚如何得到成千上萬的逗號。所以如果我輸入1200就會顯示1,200。

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

非常感謝您的幫助!

0

問題是,只有當您更改表單的值時才觸發事件。它的只讀形式,所以他們不能改變。

不管怎麼說,這是優化代碼:

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函數中的參數,因此引發的第二個代碼觸發了錯誤。

我希望這可以幫助你:)

+0

這不起作用。控制檯說以下內容: '未捕獲的ReferenceError:refenen1沒有定義 rokboxPathbudget:76 (匿名功能)的mootools-core.js:370 hmootools-core.js:33個 Array.implement.eachmootools核。 js:39 invoke.fireEventmootools-core.js:369 g' 我需要將只讀關掉嗎?我真的很想這樣,最終用戶不能改變總數。 – 2012-03-18 19:13:16

+0

它稱爲rekenen1的函數不是refenen1。我認爲你拼錯了這個名字 – ajimix 2012-03-19 13:29:13