简体   繁体   English

提交后返回动态表格

[英]Go back to dynamic form after submit

I've got a form like this: 我有这样的形式:

<form action="" method="POST">
    <input type="text" value="name[]">
    <input type="submit">
</form>
<a href="#" id="add">Add field</a>

With Javascript / jQuery there is an option to add more fields, like: 使用Javascript / jQuery,可以选择添加更多字段,例如:

$('a').click(function()
{
    $('form').append( $('input[type="text"]').clone() );
    return false;
});

It's just some dummy code, but to make clear that it's a dynamic form. 这只是一些伪代码,但需要说明的是它是动态形式。

When this form is submitted and I go back (just with the back button in the browser), the extra added fields with the values are gone! 提交此表单后,我返回(仅使用浏览器中的“后退”按钮),带有值的额外添加字段就消失了!

How to handle this easy? 如何轻松处理?

UPDATE 更新

I'm using CodeIgniter and the form validation. 我正在使用CodeIgniter和表单验证。 If the form fails all the variables will stay where they are and I've done some custom coding so the dynamic fields and there values are still there. 如果表单失败,所有变量将保留在原处,并且我已经进行了一些自定义编码,因此动态字段及其值仍然存在。 Can I use some part of this for what I'm trying to do? 我可以使用其中的一部分来做我想做的事情吗? Basically everything is already there for the form validation. 基本上,已经存在用于表单验证的所有内容。 Now this has to work with while going back to... 现在,这必须在回...

The submit button will make a new request for the document, unloading the current one. 提交按钮将对文档提出新请求,并卸载当前文档。 The easiest way to save your progress on the page is to not unload it at all. 在页面上保存进度的最简单方法是完全不卸载它。 You can find a simple implementation here 您可以在这里找到一个简单的实现

You can also try using cookies or local storage to store the state of your document. 您也可以尝试使用Cookie或本地存储来存储文档的状态。

Update I just realized you could also use 更新我刚刚意识到您也可以使用

$("form").bind('submit',function(){
   return false;
});

However this will not solve your problem if you unload the document. 但是,如果您卸载文档,这将无法解决您的问题。 And use the "back" button to re-load it. 并使用“返回”按钮重新加载它。 The only things that could save you then are cookies and local storage. 唯一可以为您节省的是Cookie和本地存储。

After searching and trying a while I've used sessionStorage for this. 搜索并尝试了一段时间后,我为此使用了sessionStorage。 Before the form submits all the values will be saved in it and when the user is clicking the back button everything is set back. 在提交表单之前,所有值都将保存在表单中,并且当用户单击“后退”按钮时,所有内容都会重新设置。 For example before submit: 例如,在提交之前:

$('form').submit(function()
{
    var array           = {};
    array['name']       = [];
    array['counter']    = 0;

    $('input').each(function()
    {
        array['name'].push( $(this).val() );
        array['counter']++;
    });

    sessionStorage.setItem('input', JSON.stringify(array));
});

And on load: 并在负载下:

if(sessionStorage.getItem('input'))
{
    var array = JSON.parse( sessionStorage.getItem('input') );

    for (var n = 0; n < (array['counter'] - 1); ++ n)
    {
        $('form').append( $('input[type="text"]').clone() );
    }

    $('input').each(function(index,value)
    {
        $(this).val( array['name'][index] );
    });

    sessionStorage.removeItem('input');
}

And I've written a blog post about this with an online demo: http://royduineveld.nl/blog/en/tips-trucs/449/waardes-behouden-na-verzenden-van-dynamisch-formulier/ 而且我已经通过在线演示撰写了有关此内容的博客文章: http : //royduineveld.nl/blog/zh-CN/tips-trucs/449/waardes-behouden-na-verzenden-van-dynamisch-formulier/

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

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