用动作切换按钮

时间:2014-05-28 06:17:12

标签: javascript jquery html css togglebutton

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

这是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; 
}

文本必须位于左侧和右侧的开关之外。左边的标签和右边的标签。

如何获取所选内容并相应显示内容?

2 个答案:

答案 0 :(得分:1)

我不确定我清楚地理解你的问题。我已经更新了你的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/

答案 1 :(得分:0)

<?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>