Javascript localStorage为复选框返回错误的值
我正在使用本地存储来恢复数据,以防用户意外地重新加载页面,但当他们重新加载时,即使他们没有选中复选框,也会选中复选框 下面是我的代码,呈现的唯一错误值是复选框。我还有其他字段,这些字段在重新加载页面时恢复的值是准确的 我想我错过了一两件事,任何帮助都将不胜感激。谢谢Javascript localStorage为复选框返回错误的值,javascript,jquery,Javascript,Jquery,我正在使用本地存储来恢复数据,以防用户意外地重新加载页面,但当他们重新加载时,即使他们没有选中复选框,也会选中复选框 下面是我的代码,呈现的唯一错误值是复选框。我还有其他字段,这些字段在重新加载页面时恢复的值是准确的 我想我错过了一两件事,任何帮助都将不胜感激。谢谢 var remember_state = { name: "rememberState", clearOnSubmit: true, _defaultNoticeDialog: function() { re
var remember_state = {
name: "rememberState",
clearOnSubmit: true,
_defaultNoticeDialog: function() {
return $("<p />", { "class": "remember_state" })
.html('Restore previously entered information? <a class="btn btn-sm btn-link" href="#"><i class="mrx far fa-history"></i>Restore</a>');
},
noticeConfirmSelector: "a",
noticeSelector: ".remember_state",
use_ids: false,
objName: false,
restoreState: function(e) {
var data = JSON.parse(localStorage.getItem(this.objName)),
$f = this.$el,
$e;
for (var i in data) {
$e = $f.find("[name=\"" + data[i].name + "\"]");
if ($e.is(":radio")) {
$e.filter("[value=\"" + data[i].value + "\"]").prop("checked", true);
}
else if ($e.is(":checkbox") && data[i].value) {
$e.prop("checked", true);
}
else if ($e.is("select")) {
$e.find("[value=\"" + data[i].value + "\"]").prop("selected", true);
}
else {
$e.val(data[i].value);
}
$e.change();
}
this.noticeDialog.remove();
e && e.preventDefault && e.preventDefault();
},
cancelNotice: function(e) {
e.preventDefault();
this.noticeDialog.remove();
},
chooseStorageProp: function() {
if (this.$el.length > 1) {
if (console && console.warn) {
console.warn("WARNING: Cannot process more than one form with the same" +
" object. Attempting to use form IDs instead.");
}
this.objName = this.$el.attr("id");
}
},
errorNoID: function() {
if (console && console.log) {
console.log("ERROR: No form ID or object name. Add an ID or pass" +
" in an object name");
}
},
saveState: function(e) {
var instance = e.data.instance;
var values = instance.$el.serializeArray();
// jQuery doesn't currently support datetime-local inputs despite a
// comment by dmethvin stating the contrary:
// http://bugs.jquery.com/ticket/5667
// Manually storing input type until jQuery is patched
instance.$el.find("input[type='datetime-local']").each(function() {
var $i = $(this);
values.push({ name: $i.attr("name"), value: $i.val() });
});
values = instance.removeIgnored(values);
values.length && internals.setObject(instance.objName, values);
},
save: function() {
var instance = this;
if (!this.saveState) {
instance = this.data(remember_state.name);
}
instance.saveState({ data: { instance: instance } });
},
bindNoticeDialog: function() {
if (!this.noticeDialog || !this.noticeDialog.length || !this.noticeDialog.jquery) {
this.noticeDialog = this._defaultNoticeDialog();
}
this.noticeDialog.on("click." + this.name, this.noticeConfirmSelector, $.proxy(this.restoreState, this));
this.noticeDialog.on("click." + this.name, this.noticeCancelSelector, $.proxy(this.cancelNotice, this));
},
setName: function() {
this.objName = this.objName || this.$el.attr("id");
if (!this.objName) { this.errorNoID(); }
},
init: function() {
this.bindNoticeDialog();
this.setName();
if (!this.objName) { return; }
this.bindResetEvents();
this.createNoticeDialog();
$(window).bind("unload." + this.name, { instance: this }, this.saveState);
}
var memory\u state={
名称:“rememberState”,
clearOnSubmit:是的,
_defaultNoticeDialog:function(){
返回$(“”,{“类”:“记住状态”})
.html('恢复以前输入的信息?');
},
注意:选择器:“a”,
通知选择器:“.记住状态”,
使用\u id:false,
objName:false,
恢复遗产:职能(e){
var data=JSON.parse(localStorage.getItem(this.objName)),
$f=这个。$el,
$e;
用于(数据中的var i){
$e=$f.find(“[name=\”“+data[i].name+“\”“]”);
如果($e.is(“:radio”)){
$e.filter(“[value=\”“+data[i].value+“\”“]”)prop(“选中”,为真);
}
else if($e.is(“:checkbox”)&&data[i].value){
$e.prop(“已检查”,正确);
}
否则,如果($e.is(“选择”)){
$e.find(“[value=\”“+data[i].value+“\”“]”)prop(“selected”,true);
}
否则{
$e.val(数据[i].值);
}
$e.change();
}
this.noticeDialog.remove();
e&&e.preventDefault&&e.preventDefault();
},
取消通知:功能(e){
e、 预防默认值();
this.noticeDialog.remove();
},
chooseStorageProp:function(){
如果(此。$el.length>1){
if(console&&console.warn){
console.warn(“警告:不能处理多个具有相同属性的表单”+
“对象。尝试改用表单ID。”);
}
this.objName=this.$el.attr(“id”);
}
},
errorNoID:function(){
if(console&&console.log){
log(“错误:没有表单ID或对象名称。添加ID或密码”+
“在对象名称中”);
}
},
保存状态:函数(e){
var instance=e.data.instance;
var values=实例。$el.serializeArray();
//jQuery当前不支持datetime本地输入,尽管存在
//dmethvin提出的相反意见:
// http://bugs.jquery.com/ticket/5667
//手动存储输入类型,直到jQuery被修补
实例。$el.find(“输入[type='datetime-local']”)。每个(函数(){
var$i=$(本);
push({name:$i.attr(“name”),value:$i.val()});
});
values=instance.removeIgnored(值);
values.length&&internals.setObject(instance.objName,values);
},
保存:函数(){
var实例=这个;
如果(!this.saveState){
instance=this.data(记住_state.name);
}
saveState({data:{instance:instance}});
},
bindNoticeDialog:函数(){
如果(!this.noticeDialog | | |!this.noticeDialog.length | |!this.noticeDialog.jquery){
this.noticeDialog=this.\u defaultNoticeDialog();
}
this.noticeDialog.on(“单击“+”this.name,this.noticeConfirmSelector,$.proxy(this.restoreState,this));
this.noticeDialog.on(“单击“+”this.name,this.noticeCancelSelector,$.proxy(this.cancelNotice,this));
},
setName:function(){
this.objName=this.objName | | this.$el.attr(“id”);
如果(!this.objName){this.errorNoID();}
},
init:function(){
this.bindNoticeDialog();
这个.setName();
如果(!this.objName){return;}
此参数为.bindResetEvents();
this.createNoticeDialog();
$(window.bind(“unload.+this.name,{instance:this},this.saveState”);
}
})
编辑:
我在
for
循环中的上执行了console.log
,并且console.log(data.value[I])
的值位于$e.is(“:复选框”)
是0
我们正在使用jquery库jquery\u memory\u state
,我的问题是他们有一个bug,类似名称的复选框会返回相同的值,即使它们有不同的值。我们使用的是simple_表单,它为复选框添加了一个隐藏值,因此在尝试恢复状态时,它会给我们带来错误
我必须做的改变是:
if ($e.is(":checkbox") && data[i].value) {
$e = $e.filter("[value=\"" + data[i].value + "\"]");
if ($e.length) {
$e.prop("checked", true);
}
}
这是一个包含完整代码的文件,以防任何人遇到此问题:
缺少的是显示,a)数据的内容和b)您在本地存储中的存储方式-您可能做错了什么™为什么它会返回其他字段的正确值?是吗?不知道它从代码中返回了什么,也不知道您是如何保存值的——也许您做错了什么™尝试在for
循环中添加一些简单的调试,例如console.log(i,data[i])
。然后,您应该能够看到data[i]的值。checkbox属性的值可能不是您期望的值data[i]。值为0,复选框的值可以是0
或1
,或true
或false
,因此我认为该值基本正常