2012-12-18 97 views
1

我該如何將此索引數組names添加到localStorage。並調用這個元素:將索引數組添加到localStorage

var name= new Array(); 

for(var i=0; i<results.rows.length; i++) { 
    names[results.rows.item(i).IdTypePrestation ] = results.rows.item(i).LibellePrestation; 
} 

如果我改變這一行:

names[results.rows.item(i).IdTypePrestation] = results.rows.item(i).LibellePrestation; 

要這樣:

names[i] = results.rows.item(i).LibellePrestation; 

一切正常,但我需要的第一線。

回答

0

設置爲localStorage

localStorage.someArrayValue = JSON.stringify(myArray); 

localStorage獲取:

myArray = JSON.parse(localStorage.someArrayValue); 
+0

它不是這樣工作:( –

+0

'localStorage ['names'] = JSON.stringify(name);' –

+0

@BMA:這是不是工作?它做什麼,你需要什麼/想要它要做什麼? – Cerbrus

0

我認爲您的解決方案應該是這樣的:

//Set names on tableau_type line of localStorage 
localStorage['tableau_type'] = JSON.stringify(names); 

//Get all names on tableau_type line of localStorage 
var retrievedObject = JSON.parse(localStorage['tableau_type']); 
$.each(retrievedObject ,function(index,element){ 
    alert(element); 
}); 
0

對於存儲陣列,http://rhaboo.org很多比把整個事情串起來更有效率。後者的問題在於,只要對它進行一些更改,就必須對整個數組進行解碼和重新編碼,例如追加一個新項目。隨着rhaboo,你會寫:

var names = [ ... whatever ... ]; 
var store = Rhaboo.persistent("Libellen"); 
if (store.libellen === undefined) 
    store.write('libellen', names); 
for (var i = 0; i < store.libellen.length; i++) 
    console.log(JSON.stringify(store.libellen[i])) 
store.libellen.push("Libelle number " + store.libellen.length); 
//Now reload and see it persist. 

順便說一句,我寫了rhaboo。