带有动作的切换按钮

Toggle button with action

本文关键字:按钮      更新时间:2023-09-26

我使用了一段代码来创建用于特定功能的切换按钮(开/关开关)。该功能包括在按钮从一个切换到另一个时,屏幕必须将数据显示为选项卡或全部。我怎么能得到的值,已点击时,切换开关。

这是html:

<div class="onoffswitch">
 <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
   <label class="onoffswitch-label" for="myonoffswitch"> 
     <span class="onoffswitch-inner"></span> 
     <span class="onoffswitch-switch"></span>
   </label>
</div>

这是css:

.onoffswitch {
position: relative; width: 35px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
display:inline-block;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #999999; border-radius: 16px;
}
.onoffswitch-inner {
display: block; 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:before, .onoffswitch-inner:after {
display: block; float: left; width: 50%; height: 12px; padding: 0; line-height: 12px;
font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "";
padding-left: 10px;
background-color: #2FCCFF; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "";
padding-right: 10px;
background-color: #2FCCFF; color: #999999;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 13px; margin: -0.5px;
background: #FFFFFF;
border: 2px solid #999999; border-radius: 16px;
position: absolute; top: 0; bottom: 0; right: 19px;
-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; 
}

文本必须位于开关的左右两侧。制表符在左边,全部在右边。

如何获得已选择的内容并相应地显示内容?

我不太明白你的问题。我已经更新了你的jsfiddle,如下所述。是你想要的东西吗?

$('#myonoffswitch').click(function(){
$('.tab').hide();
if($('#myonoffswitch').attr('checked') == "checked")
{
   $('#yes').show();  
}
else
{
    $('#no').show();
}
//alert($('#myonoffswitch').attr('checked'));
});
http://jsfiddle.net/5jDdg/1/

<?php if((@$personal_expense_itemize=="Y")||(@$personal_expense_itemize=="on")){
    $checked = 'checked';
}?>
<div>
        <div class="onoffswitch">
        <input id="myonoffswitch" class="onoffswitch-checkbox" name="personal_expense_itemize" type="checkbox" <?php echo @$checked;?>>
        <label for="myonoffswitch" class="onoffswitch-label">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
        </label>
        </div> 
        </div>