【问题标题】:Bootstrap Toggle still displaying as a checkboxBootstrap Toggle 仍显示为复选框
【发布时间】:2016-12-23 01:00:14
【问题描述】:

我正在尝试使用http://www.bootstraptoggle.com/ 实现拨动开关

我已将以下内容添加到我的页面中:

<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

并简单地使用以下方法实现切换开关:

<input id="toggle-demo" type="checkbox" checked data-toggle="toggle">

<script type="text/javascript">
$(function() {
    $('#toggle-demo').bootstrapToggle()
})
</script>

另外,这是我的捆绑配置:

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));
    }
}

但是,它仍然显示为复选框而不是切换开关,有什么想法吗?谢谢:)

【问题讨论】:

  • 控制台是否显示任何错误?
  • @mrid nope,没有控制台错误。
  • 可能是个愚蠢的问题,但你确实初始化了插件,对吧?
  • @mrid 你到底是什么意思?
  • @mrid 谢谢你 - 你让我意识到 jquery 并没有因为脚本渲染的一些奇怪原因被选中,我现在已经解决了这个问题 :)

标签: html twitter-bootstrap checkbox twitter-bootstrap-3


【解决方案1】:

当您可以使用少量代码构建自己的插件时,为什么还要选择插件?

#foo:checked::before,
input[type="checkbox"] {
    position:absolute;
    clip: rect(0,0,0,0);
    clip: rect(0 0 0 0);
}

input[type="checkbox"] + label {
    margin-left: 100px; line-height: 30px; height: 27px; cursor: pointer;
    -webkit-transition: background-position 0.3s ease-in-out;
    -moz-transition: background-position 0.3s ease-in-out;
}

#foo:checked,
input[type="checkbox"] + label::before {
    content: ""; width: 94px; height: 27px;
    background: url(http://dl.dropbox.com/u/391082/sprite2.png) -56px 0;
    position: absolute;
    border-radius: 4px;
    -webkit-transition: background-position 0.3s ease-in-out;
    -moz-transition: background-position 0.3s ease-in-out;
}

input[type="checkbox"]:checked + label::before {
    background-position: -3px 0;
}

input[type="checkbox"] + label:hover::before {
    background-position: -50px 0;
    border-right: solid 1px rgba(0,0,0,.3);
    width: 93px;
}

input[type="checkbox"]:checked + label:hover::before {
    background-position: -9px 0;
    border-left: solid 1px rgba(0,0,0,.3);
    width: 93px;
}
<input id=test type=checkbox value=test checked>
<label for=test>Label</label>

【讨论】:

  • 谢谢 - 这看起来很棒!我只是出于好奇尝试过,但再次只显示一个复选框而不是切换开关,我不知道为什么!
  • @mrid 如果你摆脱了hover 状态,切换开/关状态时口吃就会消失。
  • 是的@Press,我添加了悬停使其看起来更像一个按钮,但后来意识到没有它会更好:D
猜你喜欢
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 2013-10-25
  • 2016-01-24
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
相关资源
最近更新 更多