douzhuo5671 2013-06-18 18:17
浏览 42
已采纳

仅在提交时使用javascript保存表单状态

So. I have a Form with a lot of checkboxes. Along with that I have a piece of javascript code that is supposed to save the state of every checkbox when the user presses submit. My short and irritating problem is two things. Question: I want to save Checkbox state to cookie ONLY when I submit the form, right now it saves if I mark a checkbox and reload the page, without submitting. Im working with Javascript and Cookies, two things that Im quite new to. So Im very greatful for all help. Here is my code that I got from here:

function getStorage(key_prefix) {
    if (window.localStorage) {
        return {
            set: function(id, data) {
                localStorage.setItem(key_prefix+id, data);
            },
            get: function(id) {
                return localStorage.getItem(key_prefix+id);
            }
        };
    } else {
        return {
            set: function(id, data) {
                document.cookie = key_prefix+id+'='+encodeURIComponent(data);
            },
            get: function(id, data) {
                var cookies = document.cookie, parsed = {};
                cookies.replace(/([^=]+)=([^;]*);?\s*/g, function(whole, key, value) {
                    parsed[key] = unescape(value);
                });
                return parsed[key_prefix+id];
            }
        };
    }
}

jQuery(function($) {
    var storedData = getStorage('com_mysite_checkboxes_'); 

    $('div.check input:checkbox').bind('change',function(){
        storedData.set(this.id, $(this).is(':checked')?'checked':'not');
    }).each(function() {
        var val = storedData.get(this.id);
        if (val == 'checked') $(this).attr('checked', 'checked');
        if (val == 'not') $(this).removeAttr('checked');
        if (val == 'checked') $(this).attr('disabled','true');
        if (val) $(this).trigger('change');
    });

});

So I want to save to cookie only on submit basically.

  • 写回答

1条回答 默认 最新

  • doupi1532 2013-06-18 19:08
    关注

    Bind to the submit event of the form instead of the change event of all the checkboxes.

    Try this in place of your second function:

    jQuery(function($) {
        // bind to the submit event of the form
        $('#id-of-your-form').submit(function() {
            // get storage
            var storedData = getStorage('com_mysite_checkboxes_');
    
            // save checkbox states to cookie
            $('div.check input:checkbox').each(function() {
                // for each checkbox, save the state in storage with this.id as the key
                storedData.set(this.id, $(this).is(':checked')?'checked':'not');
            });
        });
    
    });
    
    jQuery(document).ready(function() {
        // on load, restore the checked checkboxes
        $('div.check input:checkbox').each(function() {
            // get storage
            var storedData = getStorage('com_mysite_checkboxes_');
    
            // for each checkbox, load the state and check it if state is "checked"
            var state = storedData.get(this.id);
    
            if (state == 'checked') {
                $(this).attr('checked', 'checked');
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c