简体   繁体   English

如何在切换到变量时保存关闭条件

[英]How to save on off conditions in a switch to a variable

Not sure a similar question has been asked before.不确定之前是否有人问过类似的问题。 If so please point it out.如果有请指出。

Of course I am new to this field.当然,我是这个领域的新手。 So please bear with me.所以请耐心等待。

I have a html webpage which contains 6 switches.我有一个包含 6 个开关的 html 网页。

I need to save on and off conditions of those switches into different variables.我需要将这些开关的开和关条件保存到不同的变量中。

on = 1 off = 0开 = 1 关 = 0

As an instance, switch 1 on/off conditions represents x variable.例如,开关 1 的开/关条件代表 x 变量。

 when on x=1 off x=0

switch 2 on/off conditions represents y variable.开关 2 开/关条件代表 y 变量。

 when on y=1 off y=0

my page views as follows.我的页面浏览量如下。

在此处输入图片说明

My code:我的代码:

 .switch { position: relative; display: inline-block; width: 60px; height: 34px; } .switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; } .slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:checked+.slider { background-color: #2196F3; } input:focus+.slider { box-shadow: 0 0 1px #2196F3; } input:checked+.slider:before { -webkit-transform: translateX(26px); -ms-transform: translateX(26px); transform: translateX(26px); } /* Rounded sliders .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; } */
 <h2>Toggle Switch</h2> <label name="s" class="switch"> <input type="checkbox" checked> <span class="slider"></span> </label><br><br> <label class="switch"> <input type="checkbox" checked> <span class="slider"></span> </label><br><br> <label class="switch"> <input type="checkbox" checked> <span class="slider"></span> </label><br><br> <label class="switch"> <input type="checkbox" checked> <span class="slider"></span> </label><br><br> <label class="switch"> <input type="checkbox" checked> <span class="slider"></span> </label><br><br> <label class="switch"> <input type="checkbox" checked> <span class="slider"></span> </label><br><br> <label class="switch"> <input type="checkbox"> </label> <label class="switch"> <input type="checkbox" checked> </label>

Can someone help me to save these switch conditions into different variables?有人可以帮我将这些切换条件保存到不同的变量中吗? I would really appreciate it.我真的很感激。

Thanks heaps!谢谢堆!

Create a JavaScript function to handle the switch change:创建一个 JavaScript 函数来处理开关更改:

<script type="text/JavaScript">
var switchValues = { };
function switched (switchElement) {
  switchValues[switchElement.id] = switchElement.checked;
}
</script>

Then make sure each of your checkbox switches has a unique id attribute and onclick handler assigned to your new function:然后确保您的每个复选框开关都有一个唯一的id属性和分配给新函数的onclick处理程序:

<label name="s" class="switch">
      <input type="checkbox" id="switch1" onclick="switched(this)" checked>
      <span class="slider"></span>
    </label><br><br>

    <label class="switch">
      <input type="checkbox" id="switch2" onclick="switched(this)" checked>
      <span class="slider"></span>
    </label><br><br>

    <label class="switch">
      <input type="checkbox" id="switch3" onclick="switched(this)" checked>
      <span class="slider"></span>
    </label><br><br>

    <label class="switch">
      <input type="checkbox" id="switch4" onclick="switched(this)" checked>
      <span class="slider"></span>
    </label><br><br>

    Etc...

I have added the use of LocalStorage to keep switch positions我添加了使用 LocalStorage 来保持开关位置

values are inside switchList object:值在switchList对象内:

const switchList = 
      {​ switch_1: true
      ,​ switch_2: true
      ,​ switch_3: true ​
      , switch_4: true
      ,​ switch_5: true
      ,​ switch_6: true
      }

diretly build from the input checkbox list从输入复选框列表直接构建

You also need a form!你还需要一个表格!

so when form element get any input change, by using event delegation you can set the checkbox value inside the switchList因此,当表单元素获得任何输入更改时,通过使用事件委托,您可以在 switchList 中设置复选框值

then the switchList is copied into local storage然后将 switchList 复制到本地存储中

 const switchersF = document.querySelector('form#switchers') , memoSwitch = 'SwitchStorage' , switchList = [...switchers.querySelectorAll('input[type=checkbox]')].reduce((s,el)=>{ s[el.name]=true;return s},{}) ; initSwitchs() ; switchersF.onsubmit=e=>e.preventDefault() // disable submit form ; switchersF.oninput=e=> { if (!e.target.matches('input[type=checkbox]')) return switchList[e.target.name] = e.target.checked setSwitchsMemo() // console.log( switchList ) } function initSwitchs() { let SwitchsMemo = localStorage.getItem(memoSwitch) if (!SwitchsMemo) { setSwitchsMemo() } else { for (let [key, value] of Object.entries( JSON.parse(SwitchsMemo) )) { switchersF[key].checked = value } } } function setSwitchsMemo() { localStorage.setItem(memoSwitch, JSON.stringify(switchList) ) }
 #switchers label { position: relative; display: block; float: left; clear: both; width: 60px; height: 34px; margin: .5em 1em; } #switchers input { display: none; } #switchers span { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; } #switchers span:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } #switchers input:checked+span { background-color: #2196F3; } #switchers input:checked+span:before { transform: translateX(26px); } #switchers input:focus+span { box-shadow: 0 0 1px #2196F3; }
 <h2>Toggle Switch</h2> <form id="switchers"> <label> <input type="checkbox" checked name="switch_1"> <span></span> </label> <label> <input type="checkbox" checked name="switch_2"> <span></span> </label> <label> <input type="checkbox" checked name="switch_3"> <span></span> </label> <label> <input type="checkbox" checked name="switch_4"> <span></span> </label> <label> <input type="checkbox" checked name="switch_5"> <span></span> </label> <label> <input type="checkbox" checked name="switch_6"> <span></span> </label> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM