php存储复选框的状态,php – 重新加载时保存复选框状态

如何通过会话保存复选框状态?我正在使用此jquery代码来切换选项:

$('div.check input:checkbox').bind('change',function(){

$('#'+this.id+'txt').toggle($(this).is(':checked'));

});

但是当我重新加载时,复选框将恢复为默认状态.我知道我必须使用会话,但如何使用php中的会话使复选框状态持续存在?

HTML:

Name

Reference

Show Name TEXT
Show Ref TEXT

解决方法:

纯粹在JavaScript中支持localStorage(如果可用),否则使用document.cookie.

function getStorage(key_prefix) {

// this function will return us an object with a "set" and "get" method

// using either localStorage if available, or defaulting to document.cookie

if (window.localStorage) {

// use localStorage:

return {

set: function(id, data) {

localStorage.setItem(key_prefix+id, data);

},

get: function(id) {

return localStorage.getItem(key_prefix+id);

}

};

} else {

// use document.cookie:

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] = decodeURIComponent(value);

});

return parsed[key_prefix+id];

}

};

}

}

jQuery(function($) {

// a key prefix is used for the cookie/storage

var storedData = getStorage('com_mysite_checkboxes_');

$('div.check input:checkbox').bind('change',function(){

$('#'+this.id+'txt').toggle($(this).is(':checked'));

// save the data on change

storedData.set(this.id, $(this).is(':checked')?'checked':'not');

}).each(function() {

// on load, set the value to what we read from storage:

var val = storedData.get(this.id);

if (val == 'checked') $(this).attr('checked', 'checked');

if (val == 'not') $(this).removeAttr('checked');

if (val) $(this).trigger('change');

});

});

标签:jquery,php,session,html

来源: https://codeday.me/bug/20190610/1213004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值