需要指导:将表单实例保存到本地存储

时间:2012-03-24 00:02:28

标签: javascript jquery forms local-storage

我创建了一个将表单作为JSON保存到本地存储的页面。它按原样工作,但我想扩展它以节省多天: http://daverau.info/happy/

有哪些方法可以节省多天的日记帐分录?按日期分页表单数据?创建多个表单实例?我应该首先放弃dumbFormState插件并以不同方式处理保存到本地存储吗?

我正在寻找选择,并希望有人能指出我的方向。

2 个答案:

答案 0 :(得分:1)

而不是像我在下面找到的那样保存对象数据:

[
    { "selector": "input[name=\"gratitude1\"]", "val": "" },
    { "selector": "input[name=\"gratitude2\"]", "val": "" },
    { "selector": "input[name=\"gratitude3\"]", "val": "" },
    { "selector": "textarea[name=\"journal\"]", "val": "" },
    { "selector": "input[name=\"exercise_num\"]", "val": "" },
    { "selector": "input[name=\"meditation_num\"]", "val": "" },
    { "selector": "input[name=\"kindness\"]", "val": "" },
    { "selector": "input[name=\"tdate\"]", "val": "2012-3-23" }
]

将密钥设置为当前日期:

[
    { "2012-3-22" :
        [
            { "selector": "input[name=\"gratitude1\"]", "val": "" },
            { "selector": "input[name=\"gratitude2\"]", "val": "" },
            { "selector": "input[name=\"gratitude3\"]", "val": "" },
            { "selector": "textarea[name=\"journal\"]", "val": "" },
            { "selector": "input[name=\"exercise_num\"]", "val": "" },
            { "selector": "input[name=\"meditation_num\"]", "val": "" },
            { "selector": "input[name=\"kindness\"]", "val": "" }
        ]
    },
    { "2012-3-23" :
        [
            { "selector": "input[name=\"gratitude1\"]", "val": "" },
            { "selector": "input[name=\"gratitude2\"]", "val": "" },
            { "selector": "input[name=\"gratitude3\"]", "val": "" },
            { "selector": "textarea[name=\"journal\"]", "val": "" },
            { "selector": "input[name=\"exercise_num\"]", "val": "" },
            { "selector": "input[name=\"meditation_num\"]", "val": "" },
            { "selector": "input[name=\"kindness\"]", "val": "" }
        ]
    }
]

答案 1 :(得分:0)

我可能会将日期添加到localStorage key,或者根据数据在存储的值中添加(由特殊字符或使用保留空格分隔)。

例如,实际数据可能是这样的:

"I'm feel'n pretty good today"

要添加日期,我可以添加:

"032312I'm feel'n pretty good today"

然后,只需使用substring来提取日期或数据。