(jQuery)在cookie中单击时保存复选框状态 [英] (jQuery) Save checkbox state on click in cookie

查看:80
本文介绍了(jQuery)在cookie中单击时保存复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个功能有很多话题,但我似乎无法得到它的工作。我在这个特定的案例上搜索了一些链接,并且有一些链接让我在这里,但是我似乎无法让他们工作。我做的唯一工作是:
http ://dl.dropbox.com/u/2238080/a/!old / z.htm
但是正如您所看到的,它不会存储未选中框的状态。



问候,
Ruben

解决方案

代码只是:编辑删除部分不需要的。

  $(document).ready(function(){
/ /读取当前/前一个设置
$(input.box [type = checkbox])。
if($ .cookie(name)&& $ .cookie(name)==true){
$(this).prop('checked',$ .cookie(name) );
}
});
// event management
$(input.box [type = checkbox])。change(function(){
(name)$(this).prop('checked'),{
path: '/',
过期:365
});
});
});

包括摆脱所有这些:

  $(document).ready(function(){
remember([name = 1]);
});
...

编辑:不详细版本:

  $(input.box)。each(function(){
var mycookie = $ .cookie($(this).attr(' ('mycookie&& mycookie ==true){
$(this).prop('checked',mycookie);
}
});
$(input.box)。change(function(){
$ .cookie($(this).attr(name),$(this).prop('checked') ,{
path:'/',
expires:365
});
});

工作示例: http://jsfiddle.net/R73vy/


There are a lot of topics regarding this function, nonetheless I can't seem to get it working. I've googled on this specific case and a bunch of links let me here, but strangly enough I can't seem to get them to work. The only thing I did get working was the following: http://dl.dropbox.com/u/2238080/a/!old/z.htm but as you can see, it doesn't store the state of the box is unchecked.

Regards, Ruben

解决方案

You can change ALL your code to just: EDITED to remove part unneeded.

$(document).ready( function(){
   // read the current/previous setting
    $("input.box[type=checkbox]").each(function() {
        var name = $(this).attr('name');
        if ($.cookie(name) && $.cookie(name) == "true") {
            $(this).prop('checked', $.cookie(name));
        }
    });
   // event management
    $("input.box[type=checkbox]").change(function() {
        var name = $(this).attr("name");
        $.cookie(name, $(this).prop('checked'), {
            path: '/',
            expires: 365
        });
    });
});

including getting rid of all these:

$(document).ready( function(){
    remember("[name=1]");
});
...

EDIT: less verbose version:

$("input.box").each(function() {
    var mycookie = $.cookie($(this).attr('name'));
    if (mycookie && mycookie == "true") {
        $(this).prop('checked', mycookie);
    }
});
$("input.box").change(function() {
    $.cookie($(this).attr("name"), $(this).prop('checked'), {
        path: '/',
        expires: 365
    });
});

working example: http://jsfiddle.net/R73vy/

这篇关于(jQuery)在cookie中单击时保存复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆