and this is the original version of it
代码就像:
HTML:
<fieldset class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="xmonoffswitch">
<label class="onoffswitch-label" for="xmonoffswitch">
<div class="onoffswitch-inner">
<div class="onoffswitch-active">Published</div>
<div class="onoffswitch-inactive">Unpublished</div>
</div>
<div class="onoffswitch-switch"></div>
</label>
</fieldset >
<div class="result"></div>
CSS:
.onoffswitch {
position: relative; width: 182px;
border: 0px;
-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 #FFF3E4; border-radius: 50px;
}
.onoffswitch-inner {
width: 200%; margin-left: -100%;
-moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
-o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner > div {
float: left; width: 50%; height: 41px; padding: 0; line-height: 41px;
font-size: 20px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: normal; font-style: italic;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner .onoffswitch-active {
padding-left: 14px;
background-color: #F0B78B; color: #E67817;
}
.onoffswitch-inner .onoffswitch-inactive {
padding-right: 14px;
background-color: #F0B78B; color: #999999;
text-align: right;
}
.onoffswitch-switch {
width: 38px; height: 38px; margin:1.5px;
background: #A1A1A1;
border: 2px solid #FFF3E4; border-radius: 50px;
position: absolute; top: 0; bottom: 0; right: 137px;
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
-o-transition: all 0.3s ease-in 0s; 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: #E67817;
}
这基本上是一个开关按钮,它有两种状态:已发布(On)和Unpublished(off)。我想通过JQuery获取当前选择的状态但无法获取它。有人会说出这个可能是JQuery片段吗?
答案 0 :(得分:3)
脚本应为:
function getStatus(){
// will return true in case of "ON" and false in case of "OFF"
return $("input#xmonoffswitch").is(":checked");
}
<强> DEMO 强>
希望这会有所帮助!!
答案 1 :(得分:1)
您可以通过选中此复选框的值来检查它
if($('.onoffswitch-checkbox').is(':checked'))
{
alert('Published');
}
else
{
alert('Unpublished');
}
此演示将为您提供切换按钮时的复选框状态