localStorage:log&保存,不工作

时间:2013-11-10 23:32:25

标签: javascript jquery local-storage

我学习了如何使用localstorage,这是我的代码:

$.fn.formBackUp = function(){
    if (!localStorage) {
        return false;
    }

    var forms = this;
    var datas = {};
    datas.href = window.location.href;

    forms.find('input,textarea').keyup(function(e){
        datas[$(this).attr('id')] = $(this).val();
        localStorage.setItem('VLR_formBackUp', JSON.stringify(datas));
    });

    console.log(localStorage);

}

$('form').formBackUp();

我只想记录并保存数据,但我的代码无效:/什么都没有保存

1 个答案:

答案 0 :(得分:0)

在使用所有jQuery函数之前,forms变量需要是“jQuerierized”。你这样做:var forms = $(this);

最终代码?

$.fn.formBackUp = function(){
    if (!localStorage) {
        return false;
    }

    var forms = $(this);
    var datas = {};
    datas.href = window.location.href;

    forms.find('input,textarea').keyup(function(e){
        datas[$(this).attr('id')] = $(this).val();
        localStorage.setItem('VLR_formBackUp', JSON.stringify(datas));
    });

    console.log(localStorage);

}

$('form').formBackUp();