Javascript 如何在localStorage中的mirthril.js中存储状态?

Javascript 如何在localStorage中的mirthril.js中存储状态?,javascript,local-storage,mithril.js,Javascript,Local Storage,Mithril.js,我试图在localStorage中保留复选框的状态,这是Mithril2.0的代码,但它不会在oninit()之后重新绘制复选框 var状态=错误 var ConfigPage={ 视图:函数(vnode){ 返回m('input[type=checkbox]',{ 价值:国家, onchange:function(e){ 状态=(e.target.value!=='true') if(localStorage!==未定义){ var s=JSON.stringify(状态) log(“在t1

我试图在localStorage中保留复选框的状态,这是Mithril2.0的代码,但它不会在oninit()之后重新绘制复选框


var状态=错误
var ConfigPage={
视图:函数(vnode){
返回m('input[type=checkbox]',{
价值:国家,
onchange:function(e){
状态=(e.target.value!=='true')
if(localStorage!==未定义){
var s=JSON.stringify(状态)
log(“在t1中保存状态:”,s)
localStorage.setItem('t1',s)
}                                                                       
}                                                                         
})                                                                          
}                                                                             
}                                                                               
m、 挂载(document.body,{
oninit:function(vnode){
if(localStorage!==未定义){
var v=localStorage.getItem(“t1”)
如果(v!==null){
state=JSON.parse(v)
log(“从t1加载状态:”,v)
}                                                                                                                                                                                                                                           
}                                                                           
},                                                                            
视图:函数(){
返回m(配置页面)
}                                                                             
})                                                                              

此问题是由于未正确设置checked属性造成的,以下是更正的代码

<!doctype html><head>                                                           
<meta name="viewport" content="width=device-width, initial-scale=1.0">          
<script src="//unpkg.com/mithril@next/mithril.min.js"></script>                 
</head>                                                                         
<body>                                                                          
<script>                                                                        
var state = false                                                               
var ConfigPage = {                                                              
  view: function(vnode) {                                                       
    return m('input[type=checkbox]', {                                          
      value: state,              
      checked: state,  // ADD THIS                                               
      onchange: function(e) {                                                   
        state = e.target.checked  // use the checked value
        if (localStorage !== undefined) {                                       
          var s = JSON.stringify(state)                                         
          console.log("save state in t1:", s)                                   
          localStorage.setItem('t1', s)                                         
        }                                                                       
      }                                                                         
    })                                                                          
  }                                                                             
}                                                                               
m.mount(document.body, {                                                        
  oninit: function(vnode) {                                                     
    if (localStorage !== undefined) {                                           
      var v = localStorage.getItem("t1")                                        
      if (v !== null) {                                                         
        state = JSON.parse(v)                                                   
        console.log("load state from t1:", v)                                   
      }                                                                                                                                                                                                                                           
    }                                                                           
  },                                                                            
  view: function() {                                                            
    return m(ConfigPage)                                                        
  }                                                                             
})                                                                              
</script>                                                                       
</body></html>                          

var状态=错误
var ConfigPage={
视图:函数(vnode){
返回m('input[type=checkbox]',{
价值:国家,
选中:状态,//添加此
onchange:function(e){
state=e.target.checked//使用选中的值
if(localStorage!==未定义){
var s=JSON.stringify(状态)
log(“在t1中保存状态:”,s)
localStorage.setItem('t1',s)
}                                                                       
}                                                                         
})                                                                          
}                                                                             
}                                                                               
m、 挂载(document.body,{
oninit:function(vnode){
if(localStorage!==未定义){
var v=localStorage.getItem(“t1”)
如果(v!==null){
state=JSON.parse(v)
log(“从t1加载状态:”,v)
}                                                                                                                                                                                                                                           
}                                                                           
},                                                                            
视图:函数(){
<!doctype html><head>                                                           
<meta name="viewport" content="width=device-width, initial-scale=1.0">          
<script src="//unpkg.com/mithril@next/mithril.min.js"></script>                 
</head>                                                                         
<body>                                                                          
<script>                                                                        
var state = false                                                               
var ConfigPage = {                                                              
  view: function(vnode) {                                                       
    return m('input[type=checkbox]', {                                          
      value: state,              
      checked: state,  // ADD THIS                                               
      onchange: function(e) {                                                   
        state = e.target.checked  // use the checked value
        if (localStorage !== undefined) {                                       
          var s = JSON.stringify(state)                                         
          console.log("save state in t1:", s)                                   
          localStorage.setItem('t1', s)                                         
        }                                                                       
      }                                                                         
    })                                                                          
  }                                                                             
}                                                                               
m.mount(document.body, {                                                        
  oninit: function(vnode) {                                                     
    if (localStorage !== undefined) {                                           
      var v = localStorage.getItem("t1")                                        
      if (v !== null) {                                                         
        state = JSON.parse(v)                                                   
        console.log("load state from t1:", v)                                   
      }                                                                                                                                                                                                                                           
    }                                                                           
  },                                                                            
  view: function() {                                                            
    return m(ConfigPage)                                                        
  }                                                                             
})                                                                              
</script>                                                                       
</body></html>