使用当前数据在页面刷新时呈现页面

时间:2012-09-26 08:54:44

标签: jquery ajax asp.net-mvc-3

我的索引包含呈现页面的默认值。我根据使用jquery的用户操作填充了我的视图中的下拉列表(相同的下拉列表根据用户操作包含不同的列表)。

我的问题是当我刷新页面时,使用索引中提供的默认数据恢复下拉列表。

有没有办法可以在页面刷新时将数据传递给索引,例如文本框中的数据或下拉列表中当前选定的值?

1 个答案:

答案 0 :(得分: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.
}

然后,根据您的页面初始化逻辑,只需添加一个这样的支票。

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对象,只是它只存储一个会话的数据。当用户关闭浏览器窗口时,将删除数据。

相关问题