我让此代码一个按钮,并从服务器中获取一些价值。
然后在加载时(初始化视图),我的值= 1,我需要使拨动开关自动打开。
下面的代码
var value = 1;
.switch {
position: relative;
display: inline-block;
width: 100px;
height: 34px;
}
.switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.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: #2ab934;
}
input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
-webkit-transform: translateX(68px);
-ms-transform: translateX(68px);
transform: translateX(68px);
}
.on {
display: none;
}
.on,
.off {
color: white;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
input:checked+.slider .on {
display: block;
}
input:checked+.slider .off {
display: none;
}
<div align="center" id="bit00_3">
<label class="switch">
<input type="checkbox" id="bit00_3" value="off" onclick="toggle(this);">
<div class="slider round">
<span class="on">ON</span>
<span class="off">OFF</span>
</div>
</label>
</div>
所以我需要一些JavaScript来使js中的值1切换按钮。以编程方式并且如果0处于关闭状态。
我只需要在init视图上使用它
答案 0 :(得分:1)
首先,您在同一个html页面中有2个id,我将输入更改为checkbox1
。
第二,toggle
函数不存在并且没有被使用,至少在示例中,因此我将其删除。
第三,我删除了您输入的值,因为您没有使用它,如果要获取该值,请仅使用document.getElementById("checkbox1").checked
或checkbox1.checked
(将复选框分配给var)< / p>
只需调用checkbox1.checked = {0 for unchecked and 1 for checked}
或仅调用checkbox1.checked
(将复选框分配给变量)
obs:我没有使用jquery,因为我不知道您是否在此页面上使用
var value = 1;
//you can put the checkbox in a variable,
//this way you do not need to do a javascript query every time you access the value of the checkbox
var checkbox1 = document.getElementById("checkbox1")
checkbox1.checked = value
document.getElementById("checkbox1").addEventListener("change", function(element){
console.log(checkbox1.checked)
});
.switch {
position: relative;
display: inline-block;
width: 100px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.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: #2ab934;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(68px);
-ms-transform: translateX(68px);
transform: translateX(68px);
}
.on
{
display: none;
}
.on, .off
{
color: white;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
input:checked+ .slider .on
{display: block;}
input:checked + .slider .off
{display: none;}
<div align="center" id="bit00_3">
<label class="switch">
<input type="checkbox" id="checkbox1">
<div class="slider round">
<span class="on">ON</span>
<span class="off">OFF</span>
</div>
</label>
</div>