引导模式事件未在轨道4上启动

Bootstrap modal events not firing on rails 4

本文关键字:轨道 启动 模式 事件      更新时间:2023-09-26

我有以下模态:

<div class="modal" id="pergunta-modal" >
  ...
</div>

以及以下模态事件:

$(document).ready ->
  $('#pergunta-modal').on 'shown.bs.modal', ->
   $('#tipo_pergunta').change ->
      if $(this).val() is "Resposta aberta"
        $("#alternativas").hide()
      else
        $("#alternativas").show()

问题是,该事件没有启动,我尝试将其更改为show或任何其他,不起作用。我无法通过'show.bs.modal'事件发生任何事情,我使用的是twitter bootstrap rails gem最新版本和rails 4.0.1,在chrome 37 上测试

编辑:我通过在模态分部的末尾添加javascript代码解决了这个问题,因为在创建回调时,模态还没有实例化,根据这个问题:Twitter引导程序模式事件未启动

然而,我仍然对代码质量不满意,我如何更改这一点,使代码可以放在assets文件夹中的javascript文件上?

Rails 4默认启用turbolinks。这意味着只有在重新加载整个页面时才会触发非公共document.ready事件,因此永远不会发生与shown.bs.modal事件的绑定。

我会像这个一样重写你的Coffscript

$(document).on 'shown.bs.modal', "#pergunta-modal", ->
    $('#tipo_pergunta').change ->
        if $(this).val() is "Resposta aberta"
            $("#alternativas").hide()
        else
            $("#alternativas").show()