使用localStorage保存切换开关的状态

时间:2018-01-23 09:08:06

标签: javascript html

我有一个HTML网站,它运行在两台不同的PC上,其中有一些来自metro.css的切换开关。我试图通过localStorage方法使用onclick保存其状态。

<label class="switch">
    <input type="checkbox" id="switch_104_02" checked="checked" onClick="switcherFunction(this.id)">
    <span class="check"/>
</label>

这是JS函数:

function switcherFunction(switcher_id) {
    if (switcher_id.checked == true) {
        localStorage.setItem(switcher_id, false);
        localStorage.getItem(switcher_id);
    } else {
        localStorage.setItem(switcher_id, true);
        localStorage.getItem(switcher_id);
    }
}

1 个答案:

答案 0 :(得分:0)

请尝试

<input type="checkbox" id="switch_104_02" checked="checked" onClick="switcherFunction()">

function switcherFunction(){
    if($('#switch_104_02').is(':checked') == true){
        localStorage.setItem("switch_104_02", "false");
        localStorage.getItem("switch_104_02");
    }else{
        localStorage.setItem("switch_104_02", "true");
        localStorage.getItem("switch_104_02");
    }
  }