【发布时间】:2011-09-19 22:19:09
【问题描述】:
我有一个可以帮助我创建 cookie 的 js 脚本。它保存选中的复选框,以便记住该值(在 cookie 中设置)。现在的问题是它似乎不适用于单选按钮。阅读 js 文件时,我看到输入 type=checkboxes。所以它忽略单选按钮是合乎逻辑的。
如何更改此脚本,使其不仅会选中选中的复选框,还会选中选中的单选按钮?
非常感谢
我的js文件脚本:
jQuery(document).ready(function(){
new chkRemembrance();
});
function chkRemembrance(){
this.__construct();
}
chkRemembrance.prototype = {
__construct : function(){
this.chk = this.fetchData(); // initialise array to store the checkboxes
this.init();
},
init : function(){
// first initialise all checkboxes that are checked
for(c in this.chk){
$("input[type=checkbox]#" + c).attr('checked', this.chk[c]);
}
// now make sure we fetch the checkbox events
var o = this;
$("input[type=checkbox]").change(function(){
o.saveData(this.id, this.checked);
})
},
fetchData : function(){
var r = {};
if ($.cookie('chk')){
r = JSON.parse($.cookie('chk'));
}
return r;
},
saveData : function(id,status){
this.chk[id] = status;
$.cookie('chk', JSON.stringify(this.chk));
}
}
【问题讨论】:
标签: javascript jquery checkbox radio-button