2012-09-26 236 views
0

我的索引包含呈現頁面的默認值。我已經根據用戶使用jquery的動作在我的視圖中填充了下拉列表(同一個下拉列表根據用戶操作包含不同的列表)。使用當前數據在頁面刷新上呈現頁面

我的問題是,當我刷新頁面時,使用索引中提供的默認數據恢復下拉列表。

有沒有什麼辦法可以將數據傳遞給頁面刷新的索引,如從文本框中的數據或當前在下拉列表中選擇的值?

回答

1

這應該很容易使用存儲或cookie ... 每次更改時,都會將所需的列表,值和所需內容存儲在Cookie中。在頁面刷新時,檢查cookie是否存在,如果它確實從cookie中獲取列表而不是默認值。

if(typeof(Storage)!=="undefined"){ 
    localStorage.dropdownlist = thelist; // The list being your list of options. 
}else{ 
    //Storage not supported, use cookie logic instead. 
} 

然後,根據您的頁面init邏輯,只需添加一個這樣的檢查。

if(typeof(Storage)!=="undefined" && typeof(localStorage.dropdownlist)!=="undefined"){ 
    populateDropDown(localStorage.dropdownlist); // The function being the same as when jQuery calls it. 
}else{ 
    //Storage not supported, use cookie logic instead. 
} 

localStorage對象存儲沒有過期日期的數據。瀏覽器關閉時數據不會被刪除,並且將在第二天,一週或一年內提供。 您可能需要考慮使用sessionStorage。 sessionStorage對象與localStorage對象相同,只不過它僅存儲一個會話的數據。數據在用戶關閉瀏覽器窗口時被刪除。

+0

更正localStorage中的資本S – Salketer