Rails动画切换开关

时间:2014-02-08 17:02:41

标签: css ruby-on-rails checkbox controller switch-statement

我正在尝试在rails视图中实现css动画切换开关,但没有成功。 我想在触发开关时将动作发布到我的rails控制器。

这是我的代码: 在我看来:

<div class="onoffswitch">
<%= check_box 'onoffswitch', '0', id:"myonoffswitch", class: 'onoffswitch-checkbox'%>
    <label class="onoffswitch-label" for="myonoffswitch">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
    </label>
</div>

当触发开关时我想要它做这个帖子:

<%= link_to "switch",{controller: "static_pages", action: "local_switch"}, method: :post %>

然后我的css基于'http://proto.io/freebies/onoff/':

.onoffswitch {
    position: relative; width: 50px;
    -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: 50% solid #CCCCCC; border-radius: 22px;
}

.onoffswitch-inner {
    width: 200%; margin-left: -100%;
    -moz-transition: margin 0.2s ease-in 0s; -webkit-transition: margin 0.2s ease-in 0s;
    -o-transition: margin 0.2s ease-in 0s; transition: margin 0.2s ease-in 0s;
}

.onoffswitch-inner:before, .onoffswitch-inner:after {
    float: left; width: 50%; height: 5px; padding: 0; line-height: 5px;
    font-size: 16px; 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: #3498DB; color: #FFFFFF;
}

.onoffswitch-inner:after {
    content: "";
    padding-right: 10px;
    background-color: #CCCCCC; color: #FFFFFF;
    text-align: right;
}

.onoffswitch-switch {
    width: 20px; margin: -7.5px;
    background: #FFFFFF;
    border: 2px solid #CCCCCC; border-radius: 22px;
    position: absolute; top: 0; bottom: 0; right: 43px;
    -moz-transition: all 0.2s ease-in 0s; -webkit-transition: all 0.2s ease-in 0s;
    -o-transition: all 0.2s ease-in 0s; transition: all 0.2s ease-in 0s; 
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个ajax代码......

$("#myonoffswitch").change(function(){
        if ($(this).is(":checked"))
        {
           $.ajax({
            type    : "POST",
            url     : "/static_pages/local_switch", # add your correct url
            dataType: 'script',
            data    : { id:2 },  #u can pass any no of data
            success : function(data){
                            }
            });
        }
        else{

        }
});

当选中复选框或切换为开启

时,将发送此ajax请求