简体   繁体   English

使用AJAX表单的HTML5历史记录

[英]HTML5 history with AJAX form

I am trying to implement HTML5 history with an AJAX form. 我正在尝试使用AJAX表单实现HTML5历史记录。

The form contains some radio buttons and dropdowns. 表单包含一些单选按钮和下拉列表。 Upon changing any of these inputs, the form is automatically submitted and results are returned via AJAX. 更改任何这些输入后,表单将自动提交,结果将通过AJAX返回。

Now having implemented history, the URL gets updated, so it looks like this for example: 现在已经实现了历史记录,URL会更新,所以它看起来像这样:

/currencies?type=usd&year=2015

Here is how I perform the AJAX and update the URL: 以下是我执行AJAX和更新URL的方法:

$('#currency-form input, #currency-form select').change(function() {
    var form = $('#currency-form');
    var url = form.attr('action');
    var data = form.serialize();

    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        data: data,
        success: function(response) {
            // update the page content
            processResponse(response.data);

            // update the page url
            window.history.pushState({}, response.meta_title, response.new_url);
        }
    });
});

To detect the back button, I have done the following: 要检测后退按钮,我已完成以下操作:

$(window).on('popstate', function(event) {
    var state = event.originalEvent.state;

    if (state !== null) {
        console.log(state);
    }
});

There is one thing I am struggling with. 我正在努力解决一件事。 Upon pressing the back button, it should pre-select the previous form values and submit the form again. 按下后退按钮后,应预先选择以前的表格值并再次提交表格。

Does anybody know how I can achieve this? 有谁知道我怎么能做到这一点?

1- HTML5 Tag Method: 1- HTML5标记方法:

Use Autocomplete tag for the form, HTML5, but can't be sure about all browsers' compatibility. 对表单HTML5使用Autocomplete标记,但无法确定所有浏览器的兼容性。

<form action="" method="post" autocomplete="on">

Check this post 查看这篇文章

2- jQuery Method: 2- jQuery方法:

See this example: Using jQuery plugin for cookies. 请参阅此示例:使用jQuery插件进行Cookie。

To set a cookie; 设置cookie; use something like this example: 使用类似这样的例子:

(function(){
// get elements values
var checkbox1 = $('#checkbox1').val();
var radio1 = $('input[id="radio1"]:checked').val();
var textbox1 = $("#textbox1").val()
//save elements values into cookies
$.cookie("radio1", checkbox1);  
$.cookie("checkbox1", radio1);  
$.cookie("textbox1", textbox1);  

});

Then to load the values upon your desired event: 然后在您想要的事件上加载值:

(function (){
//read value from cookie.
var radio1= $.cookie("radio1");
var checkbox1= $.cookie("checkbox1");
var textbox1= $.cookie("textbox1");
$('#radio1').prop('checked', radio1);
$('#checkbox1').prop('checked', checkbox1);
$('#textbox1').val(textbox1);


// Etc...
}); 

When submission or on live editing; 提交或实时编辑时; try to save the entry in cookies and when you back to the form...load that values into fields. 尝试保存cookie中的条目,当您返回到表单时...将这些值加载到字段中。

3- Populate from JSON Data: 3-从JSON数据填充:

Reference: 参考:

// reset form values from json object
$.each(data, function(name, val){
    var $el = $('[name="'+name+'"]'),
        type = $el.attr('type');

    switch(type){
        case 'checkbox':
            $el.attr('checked', 'checked');
            break;
        case 'radio':
            $el.filter('[value="'+val+'"]').attr('checked', 'checked');
            break;
        default:
            $el.val(val);
    }
});

The way to accomplish this is to push the new input field values in to state as follows: 完成此操作的方法是将新输入字段值推入state ,如下所示:

var state = {
    field1: $('#input1').val(),
    field2: $('#input2').val()
};

window.history.pushState(state, response.meta_title, response.new_url);

Then when you detect back button event, do the following to repopulate the input fields: 然后,当您检测到后退按钮事件时,请执行以下操作以重新填充输入字段:

$('#input1').val(state.field1);
$('#input2').val(state.field2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM