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

纯粹在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');

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值