This 当I此值返回值时,本地存储值为[object object]。

This 当I此值返回值时,本地存储值为[object object]。,this,local-storage,This,Local Storage,我试图获取键1的本地存储值,设置为选中的复选框的值,但javascript函数返回的值在本地存储中显示为[object object]。任何帮助都将不胜感激,提前谢谢你 <html> <head> <script type="text/javascript"> function topic_1_status(){ var status = { get_value: function(){ return this.value;

我试图获取键1的本地存储值,设置为选中的复选框的值,但javascript函数返回的值在本地存储中显示为[object object]。任何帮助都将不胜感激,提前谢谢你

<html>

<head>

<script type="text/javascript">


function topic_1_status(){

var status = {
    get_value: function(){
         return this.value;
                    }

            };

localStorage.setItem(1, status);


}


</script>

</head>

<body>

<div>Topic no.1   </div>
<form action="">
<input type="checkbox" value="learnt" onclick="topic_1_status()" /> Learnt
<input type="checkbox" value="memorised" onclick="topic_1_status()" /> Memorised
<input type="checkbox" value="understood" onclick="topic_1_status()" /> Understood
<input type="checkbox" value="unlearned" onclick="topic_1_status()" /> Unlearned


</body>

</html>

函数主题_1_状态(){
风险值状态={
获取_值:函数(){
返回此.value;
}
};
setItem(1,状态);
}
专题1
学会
记住
理解
未学习

您的代码正在将值设置为状态。状态是一个复杂的对象。本地存储只能存储字符串。通常,您需要事先序列化为JSON,但这并不是您真正想要的。我认为您只需要执行localStorage.setItem(1,this.value);但请注意,您绑定到4个不同的复选框,并且总是写入一个值“1”。我认为您不想这样做。

您得到的是“object object”,因为您存储的是对象“status”,而不是复选框的值

这些修改应该会给你想要的结果

 **On the html where you call your javascript** 
 <input type="checkbox" value="learnt" onclick="topic_1_status(this);" />
 **As you can see above im passing 'this' to the function

还提到,似乎您希望使用单选按钮而不是复选框,因为您计划一次只存储一个项目

谢谢,这已经奏效了。我现在也使用单选按钮。
 //On the javascript
 function topic_1_status(elem){
     if(!elem.checked){//You may also want to check that you are checking it rather than unchecking
        var checkboxValue = elem.value;
        localStorage.setItem(1,checkboxValue);
     }
 }