2013-04-22 41 views
2

這是我我有什麼: enter image description hereLive搜索的localStorage的(在陣列搜索)

,現在我想做出實時搜索。重點是當你輸入文本框時,你必須在本地存儲中搜索與搜索框中文本相同的東西。所有不符合的數據都不會消失。

$("#searchbox").keyup(function(){ 

    // Retrieve the input field text 
    var searchtext = $(this).val(); 

    // Loop through the local storage 
    var a = {}; 
    a = JSON.parse(localStorage.getItem('session')); 

    alert(a); 
}); 

正如你可以看到我做的警告和我的輸出是這樣的:

{"21114":{"id":"21114","external_id":"","sessiongroupid":"1844","eventid":"5588","order":"0","name":"localStorage 
HTML5 Session","description":"localstorage","starttime":"2013-04-23 
12:00:00","endtime":"2013-04-23 
13:30:00","speaker":"","location":"","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"},"21115":{"id":"21115","external_id":"","sessiongroupid":"1845","eventid":"5588","order":"0","name":"tweede","description":"tweede","starttime":"2013-04-03 
00:00:00","endtime":"2013-04-04 
00:00:00","speaker":"","location":"","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":"","venueid":"0"}} 

現在我想對名稱進行搜索。有人可以幫助我如何做到這一點?

回答

1

在對象上簡單地重複

var json = JSON.parse(localStorage); 

for(obj in json) { 
    console.log(json[obj].name); //compare this with your "searchtext" 
} 

打印

localStorage HTML5 Session 
tweede 

你可以,如果.name匹配任何你想要的並進行處理的循環中只需檢查。