html - 确定 On/Off 开关是 On 还是 Off
问题描述
这是我的 html 开关:
$('#myonoffswitch').click(function () {
alert($('#myonoffswitch').val());
});
.onoffswitch {
position: relative;
width: 77px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
border: 2px solid #8A8585;
border-radius: 11px;
}
.onoffswitch-inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block;
float: left;
width: 50%;
height: 26px;
padding: 0;
line-height: 26px;
font-size: 16px;
color: white;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 5px;
background-color: #282828;
color: #0C7A22;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 5px;
background-color: #282828;
color: #FF0000;
text-align: right;
}
.onoffswitch-switch {
display: block;
width: 34px;
margin: -4px;
background: #282828;
position: absolute;
top: 0;
bottom: 0;
right: 47px;
border: 2px solid #8A8585;
border-radius: 11px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
background-color: #282828;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" />
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
为什么总是alert($('#myonoffswitch').val());
显示on
?
如何确定 On/Off 开关是 On 还是 Off
解决方案
.is(":checked")
代替.val()
: _
$('#myonoffswitch').click(function () {
alert($('#myonoffswitch').is(":checked"));
});
.onoffswitch {
position: relative;
width: 77px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
border: 2px solid #8A8585;
border-radius: 11px;
}
.onoffswitch-inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block;
float: left;
width: 50%;
height: 26px;
padding: 0;
line-height: 26px;
font-size: 16px;
color: white;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 5px;
background-color: #282828;
color: #0C7A22;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 5px;
background-color: #282828;
color: #FF0000;
text-align: right;
}
.onoffswitch-switch {
display: block;
width: 34px;
margin: -4px;
background: #282828;
position: absolute;
top: 0;
bottom: 0;
right: 47px;
border: 2px solid #8A8585;
border-radius: 11px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
background-color: #282828;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" />
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
推荐阅读
- javascript - 为什么“await”总是需要在“async”函数中使用
- python - 如何在pytorch中将字符串列表转换为张量?
- react-apollo - 使用HttpLink时客户端的apollo-client reactjs 404(未找到)
- javascript - 在 Observable 中使用 Promise
- mongodb - MongoDB聚合转换
- reactjs - React Quill 与 AntDesign 表单
- mysql - 恢复文件夹 /var/lib/mysql 的内容后我看不到数据库
- ios - 是否可以自定义 UITableView 的重新排序图标以位于左侧?
- python-3.x - Vagrant 上的“HeadLess”模式“Runtime.executionContextCreated 具有无效的‘上下文’”中的 Selenium
- c# - 是否可以在控制台中调整线之间的距离?