【问题标题】:Bootstrap Switch Event Handler not working引导开关事件处理程序不起作用
【发布时间】:2014-04-23 02:15:23
【问题描述】:

我问了一个类似于here 的问题,但现在我坚持使用 Bootstrap Switch 的event handler。我只想在切换开关时打印出状态。目前只在点击时切换状态,不会触发新事件。

以下是我的一些设置:

一些.js

$(document).ready(function() {
  $("#famous-people-switch").bootstrapSwitch();

  $("#famous-people-switch").on("switchChange", function (e, data) {
    console.log(data.value);
  });
});

some.html.erb

<div class="col-md-6">
  <input id="famous-people-switch" type="checkbox" name="famous-people" checked>
  <h4>Famous People:</h4>
</div>

宝石

Using sass (3.3.2)
Using bootstrap-sass (3.1.1.0)
Using bootstrap-switch-rails (2.0.2)
Using jquery-rails (3.1.0)
Using sass-rails (4.0.1)

Application.css.scss

@import "bootstrap";
@import "bootstrap3-switch";

Application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require bootstrap-switch
//
//= require_tree .

【问题讨论】:

  • 对我来说似乎工作正常 - jsfiddle.net/tonicboy/S52bm。请注意,您的 HTML 中有几个错误:1) "checked" 不是最小化属性,它必须是 checked="checked" 和 2) &lt;input /&gt; 是一个自闭合标签,因此在末尾需要正斜杠。
  • @TNguyen 我的 Rails 设置有问题吗?我将 html 更新为有效,但它仍然无法正常工作。它变成了 Bootstrap Switch 切换按钮,但事件处理程序不起作用。
  • 这听起来像是语法错误或配置问题,但如果没有更多细节,这里的任何人都很难确定确切的问题。你尝试过其他活动吗?你是否也在代码中设置了断点,看是否到达console.log语句?
  • 我想我找到了问题所在。看起来我正在使用的 gem (bootstrap-switch-rails) 还没有更新到版本 3。我正在分叉并提交一个拉取请求来修复它!感谢您的帮助

标签: javascript jquery ruby-on-rails twitter-bootstrap


【解决方案1】:

由于 bootstrap-switch 的版本,该事件没有被触发。版本 2 不包含它们,并且 gem 当前仅支持版本 2。我已经提交了一个 pull request,将 gem 更新到版本 3(仍在开发中)。

第 3 版分支https://github.com/tomprats/bootstrap-switch-rails/tree/allow-switch-v3

拉取请求https://github.com/manuelvanrijn/bootstrap-switch-rails/pull/9

用法

更新您的 Gemfile 以使用正确的分支:

gem "bootstrap-switch-rails", git: "git://github.com/tomprats/bootstrap-switch-rails, branch: "allow-switch-v3"

更新您的 Application.js 文件:

//= require bootstrap-switch3

更新您的 Application.css 文件:

*= require bootstrap-switch3

或 Application.css.scss 文件:

@import "bootstrap-switch3";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多