我可以保存序列化的表单数据吗?

问题描述:

In my continued effort to understand hash tags and page navigation I'm hitting a road block with resubmitting form data if a user uses the browsers navigation buttons (e.g. back and forward).

My function that uses the form data is below:

if(page == "/visits_results") {

    $('#start').val(start);

    $.post("visits_results.php", $("#profile_form_id").serialize(),
    function(data) {
        $('#search_results').html(data);
        location.href = "#visits_results=" + start; 
    });
}

This works fine and dandy if the form is still visible, for instance if I use pagination it performs as I would expect.

My issue is when the user clicks the browsers back button (the form has now been removed) and then they click the browsers forward button. The event gets triggered but my serialized form data is now empty. Is there any way to cache the form data so I can continue to call it?

Any thoughts?

在我继续努力理解哈希标签和页面导航的过程中,我正在重新提交表格数据,如果是 用户使用浏览器导航按钮(例如后退和前进)。 p>

我使用表单数据的功能如下: p>

  if(  page ==“/ visits_results”){
 
 $('#start')。val(start); 
 
 $ .post(“visits_results.php”,$(“#profile_form_id”)。serialize(  ),
 function(data){
 $('#search_results')。html(data); 
 location.href =“#visits_results =”+ start; 
}); 
} 
  代码>  pre> 
 
 

如果表单仍然可见,这可以正常工作,例如,如果我使用分页,它会像我期望的那样执行。 p>

My 问题是当用户点击浏览器后退按钮(表单现已被删除)然后他们点击浏览器前进按钮。 事件被触发但我的序列化表单数据现在为空。 有没有办法缓存表单数据,以便我可以继续调用它? p>

有什么想法吗? p> div>

You can save data fairly easily using localStorage like http://jsfiddle.net/zDPjm/

Your best bet (I think) would be to store the serialized data in a cookie. When the user returns to the page which he/she has already filled out, retrieve the data from the cookie, unserialize it, and place it back where it belongs.

Cookies are fairly trivial to work with, there is a decent cross-browser implementation for reading/writing cookies on quirksmode:

or if you'd prefer to use a plugin:

Well, sure, you could do something very simple such as:

if ($("#profile_form_id").serialize() != "") serializedFormData = $("#profile_form_id").serialize();
$.post("visits_results.php", serializedFormData, /* Rest of Code */