存儲陣列我在我的代碼中的錯誤,僅保存最後一個對象在重新加載陣列。我有一種感覺,我的addAccount()函數沒有正確保存或插入數據。其他一切正常工作。在我的控制檯中,它顯示數據正被插入到數組中,但是當我刷新時,我只獲取最後一個保存的對象。 我不知道該怎麼做。 在localStorage的錯誤
// The list of accounts array.
var accountsArray = [];
function addAccount() {
\t // Take fields and put user data into varables.
\t var accountName = document.getElementById('accountName').value;
var accountBalance = document.getElementById('accountBalance').value;
var accountType = document.getElementById("accountType");
\t var accountTypeSelected = accountType.options[accountType.selectedIndex].text;
\t var accountCurrency = document.getElementById("accountCurrency");
\t var accountCurrencySelected = accountCurrency.options[accountCurrency.selectedIndex].text;
\t
\t var temporaryObject = {
\t \t 'accountName': accountName,
\t \t 'accountBalance': accountBalance,
\t \t 'accountTypeSelected': accountTypeSelected,
\t \t 'accountCurrencySelected': accountCurrencySelected
\t };
\t
\t accountsArray.push(temporaryObject);
\t console.log(accountsArray);
\t
\t saveAccountData();
showAccountsArray();
}
function saveAccountData() {
\t localStorage.setItem('accountsArray', JSON.stringify(accountsArray));
}
function showAccountsArray() {
\t //var accountsLocalStorage = JSON.parse(localStorage['accountsArray']);
\t if (localStorage.getItem("accountsArray") === null) {
\t \t document.getElementById("getStarted").style.visibility="visible";
\t \t document.getElementById("balanceToolbarName").style.visibility="hidden";
\t \t document.getElementById("accountsMainList").style.visibility="hidden";
\t } else {
\t
\t \t var accountsLocalStorage = JSON.parse(localStorage['accountsArray']);
\t \t console.log(accountsLocalStorage);
\t \t
\t \t var accountInfo = '';
\t \t var i = 0;
\t \t while (i < accountsLocalStorage.length) {
\t \t \t accountInfo += '<li class="swipeout"><a href="#" class="item-link"><div class="swipeout-content item-content"><div class="item-inner"><div class="item-title">' + accountsLocalStorage[i].accountName + '</div><div class="item-after">$' + accountsLocalStorage[i].accountBalance + '</div></div></div><div class="swipeout-actions-left"><a href="#" class="action1">Clear</a></div><div class="swipeout-actions-right"><a href="#" class="action1">Delete</a></div></a></li>';
\t \t \t document.getElementById("accountsList").innerHTML = accountInfo;
\t i++;
\t \t }
\t \t
\t \t document.getElementById("getStarted").style.visibility="hidden";
\t \t document.getElementById("balanceToolbarName").style.visibility="visible";
\t \t document.getElementById("accountsMainList").style.visibility="visible";
\t }
\t
}
凡在你的代碼,你叫'addAccount()'?在你調用saveAccountData()之前,你是否多次調用它?我相信saveAccountData會破壞你之前放在那裏的任何東西,所以,如果你在調用'saveAccountData()'之前多次調用'addAccount()'''''''''''' –
@GeniaS。我很明白你在說什麼。我在我的網站上發佈了所有代碼 - 這個應用程序是一個web應用程序[link to web,balance pro](http://www.jordankennedy.com/balancepro/index.html) – jryankennedy
@GeniaS。如果'accountsArray'是一個全局變量,那麼可能會發生什麼? 'saveAccountData'店無論是'accountsArray'變量裏面......所以,我認爲你的理論並不能解釋爲什麼'saveAccountData'被保存的最後一個項目 – Leo