Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-Twitter引导后触发的两个事件&x27;s模式关闭_Javascript_Jquery_Ruby On Rails_Twitter Bootstrap_Modal Dialog - Fatal编程技术网

Javascript jQuery-Twitter引导后触发的两个事件&x27;s模式关闭

Javascript jQuery-Twitter引导后触发的两个事件&x27;s模式关闭,javascript,jquery,ruby-on-rails,twitter-bootstrap,modal-dialog,Javascript,Jquery,Ruby On Rails,Twitter Bootstrap,Modal Dialog,我的代码 我正在使用模态来显示我的一个视图,如下所示: # lessons/index.haml %a{href: new_lesson_event_path(lesson), "data-toggle" => "modal", "data-target" => "#myModal"} ="Book now" 单击此按钮时,将在用户所在的当前视图顶部呈现一个模式 我的模式最初是在应用程序视图级别定义的: # layouts/application.haml #myModal

我的代码

我正在使用模态来显示我的一个视图,如下所示:

# lessons/index.haml

%a{href: new_lesson_event_path(lesson), "data-toggle" => "modal", "data-target" => "#myModal"}
  ="Book now"
单击此按钮时,将在用户所在的当前视图顶部呈现一个模式

我的模式最初是在应用程序视图级别定义的:

# layouts/application.haml

#myModal.modal.fade{:tabindex => "-1", style:'margin-top:50px'}
  .modal-dialog.modal-lg
    .modal-content
视图在模式内容中呈现,如下所示:

# events/new.haml

.modal-header
  %button.close{"aria-label" => "Close", "data-dismiss" => "modal", :type => "button"}
    %span{"aria-hidden" => "true"} ×
  %h4.modal-title
    ="Sign up for #{@lesson.team_name}"
.modal-body
  # my view content
.modal-footer
  %button.btn.btn-default{"data-dismiss" => "modal", :type => "button"} Close

我的问题

这一切都很好,但每当我关闭模式时,我的原始
lessons/index
视图中的JavaScript似乎在顶部添加了另一组事件侦听器


当前调试状态

我在
lessons/index
上有一个
slideToggle()
事件,每当我关闭模式时,该事件会被触发两次。当我多次打开和关闭modal时,它似乎只触发两次。。。当模式最初在应用程序级别上定义时

我尝试了多个不同的行,它们执行类似于
$(this).off()
$(this.removeEventListener(stuff)
的操作,每当启动
hidden.bs.modal
时,都会清除似乎每次都会抛出的脚本。。。但都不管用

# this event is fired when the modal is closed... Only fires once every time.
$('#myModal').on 'hidden.bs.modal', (e) ->
  console.log('hidden event fired' + JSON.stringify(e, undefined, '\t'))
  $(this).off()
  $("#myModal").off()
如果我将外部模式内容从
application.haml
移动到
lessons/index.haml
,事件侦听器似乎堆叠在一起,并且
slideToggle()
事件触发的次数随着我打开和关闭模式的次数越来越多而增加(内部模态内容仍保留在模态视图中
事件/new

从这一切我可以得出的结论是,事件侦听器正在堆叠,我不知道如何取消堆叠。有什么建议吗?我可以做更多的测试吗?

一种方法是使用


这将阻止父元素在调用模式关闭后触发其事件。这可能值得一试。

我尝试在
$(“.more info btn”)之后使用
e.stopprogation()
。在“click”(单击),(e)->
,但它仍然触发了两次。
e.isproprogationstopped()
返回
true
。您是否建议
stopPropogation()
$('#myModal')上。在'hidden.bs.modal'(e)->
触发器上?是的,在模式的关闭事件上,我知道问题来自那里。
…但每当我关闭模式时…