【问题标题】:Use of localStorage and checkbox(switch)localStorage 和 checkbox(switch) 的使用
【发布时间】:2018-08-13 15:23:31
【问题描述】:

首先。我非常感谢您的帮助。我是 JS 的初学者,所以即使这是一个愚蠢的问题也请不要笑。我想要做的是在我的应用程序中使用 ios 样式开关,我希望它们保持设置的值。例如,如果用户关闭,那么即使我重新加载页面,开关也应该保持关闭状态。我在下面准备了一些代码

HTML

 <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
 <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

 <input id="toggle-one" checked type="checkbox">

JS

<script>
  $(function() {
 $('#toggle-one').bootstrapToggle();
  })
</script>

非常感谢

【问题讨论】:

标签: javascript html local-storage


【解决方案1】:
$(function() {
  if(localStorage){
    $('#toggle-one').prop("checked",localStorage["ck"]==1);
  }
  $("#toggle-one").on("change",function(e){
    window.localStorage["ck"] = e.target.checked?1:0;
  })
})

jsBin

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 2018-04-14
    • 2020-11-21
    • 2022-06-30
    • 2023-03-14
    • 2011-11-02
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多