使用cookie的值重新填充表单

时间:2013-04-24 19:00:13

标签: javascript jquery cookies

我有一个表单,可以在cookie中保存表单值,用户决定不完成表单。 这是我到目前为止创建cookie并读取cookie

function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function string2form($form, serializedStr) {
var fields = JSON.parse(serializedStr);
for (var i = 0; i < fields.length; i++) {
    var controlName = fields[i].name;
    var controlValue = fields[i].value;
    $form.find(['name='+ controlName]).val(controlValue);
}
}

jQuery('.rsform-button').click(function() {   
    var cookie = readCookie('storyCookie');     
    if(cookie === null){
        createCookie('storyCookie',JSON.stringify(jQuery('#userForm').serializeArray()),365);
    } 
    else{
        string2form(jQuery('#userForm'), cookie);
    }
});

我要做的是检查cookie是否存在以及它是否使用cookie中的值重新填充表单。 cookie格式如下

[{"name":"form[story description]","value":"input value 1"},{"name":"form[star]","value":"input value 2"},{"name":"form[supporting characters]","value":"input value 3"},{"name":"form[capture your thoughts]","value":""},{"name":"form[timelines]","value":""},{"name":"form[how many settings]","value":""},{"name":"form[specific event]","value":""},{"name":"form[opportunity for growth]","value":""},{"name":"form[lessons learned]","value":""},{"name":"form[recurring themes]","value":""},{"name":"form[things done differently]","value":""},{"name":"MAX_FILE_SIZE","value":"0000"},{"name":"form[todays date]","value":""},{"name":"form[name]","value":""},{"name":"form[title]","value":""},{"name":"form[address]","value":""},{"name":"form[city]","value":""},{"name":"form[state]","value":""},{"name":"form[zip]","value":""},{"name":"form[telephone]","value":""},{"name":"form[email]","value":""},{"name":"form[signature]","value":""},{"name":"form[captcha]","value":""},{"name":"form[formId]","value":"4"}] 

我想知道如何将其拆分以获取表单字段的名称和该字段的值,并使用密钥对重新填充表单。好的,感谢@Barmar指点我这个方向。好的,现在我无法使用我从这篇文章jQuery Storage and retrieval form state (with data)获得的代码来重新填充表单。 我一直在撞墙试图让这个工作起来,但我仍然没有运气。我有一段时间没有做任何编码所以我生锈了。

1 个答案:

答案 0 :(得分:0)

这就是我用来从保存的Cookie中自动填充表单的内容

var form = $('.post-610 form');
if (form.length) {
    var form_data = readCookie('saved_form');
    if (form_data != null) {
        string2form(form, form_data);
    }

    form.submit(function() {
        var form_data = form2string(form);
        createCookie('saved_form', form_data, 5);
    });
}

function createCookie(name, value, days) {
    var expires;

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = encodeURIComponent(name) + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ')
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) === 0)
            return decodeURIComponent(c.substring(nameEQ.length, c.length));
    }
    return null;
}


function form2string($form) {
  return JSON.stringify($form.serializeArray());
}

function string2form($form, serializedStr) {
  var fields = JSON.parse(serializedStr);
  for(var i = 0; i < fields.length; i++){
    var controlName = fields[i].name;
    var controlValue = fields[i].value;
    if (controlValue != '' && $form.find('[name=' + controlName + ']').val() == ''){
        $form.find('[name=' + controlName + ']').val(controlValue);
    }
  }
}