简体   繁体   English

在Rails的确认对话框中单击“取消”时触发“单击”事件

[英]Triggering a 'click' event when cancel is clicked on a confirmation dialogue box in rails

I am using this gem ( https://github.com/bluerail/twitter-bootstrap-rails-confirm ) to replace the standard browser confirmation dialogue box with a bootstrap modal, and it is working, however, there are no built-in callbacks for when the 'Confirm' or 'Cancel' buttons or selected, so I am trying to create my own. 我正在使用这个gem( https://github.com/bluerail/twitter-bootstrap-rails-confirm )用引导程序模式替换标准浏览器确认对话框,并且它可以工作,但是,没有内置的当“确定”或“取消”按钮被选中时回调,所以我试图创建自己的按钮。

I am currently disabling the original link when it is clicked to prevent the user from clicking it twice, however, if 'Cancel' is clicked on the confirmation dialogue box, the link needs to be re-enabled. 我当前正在单击原始链接时将其禁用,以防止用户单击它两次,但是,如果在确认对话框中单击“取消”,则需要重新启用该链接。 This is what I tried: 这是我尝试的:

$('#confirmation_dialog').find('.cancel').on 'click', (e) ->
    console.log('cancel was clicked')
    $('.disabled').removeClass('disabled')

The issue is that this is not run when the 'Cancel' button/link is clicked. 问题是,单击“取消”按钮/链接时,该命令无法运行。

When using the Chrome JS console, I have no problem finding the correct 'Cancel' button on the modal, and calling it using 使用Chrome JS控制台时,我在模态上找到正确的“取消”按钮并使用进行调用时没有问题

$('#confirmation_dialog').find('.cancel').click()

which effectively cancels and closes the modal, however, my on click event is not triggered, so the button is not re-enabled. 它可以有效地取消和关闭模式,但是不会触发我的on click事件,因此不会重新启用该按钮。

This is the link that is on the page for the user to click: 这是页面上供用户单击的链接:

<td><%= link_to 'Cancel Appt', cancel_appointment_path(appointment), class: 'btn btn-default btn-xs btn-danger btn-disable-after-click', data: { confirm: "Are you sure you want to cancel the appointment?", commit: 'Yes', cancel: 'No' } %></td>

and this disables the button so it cannot be clicked twice by the user: 并且禁用了该按钮,因此用户无法两次单击它:

$('.btn-disable-after-click').on 'click', (e) ->
    $(this).addClass('disabled')

Any suggestions on what I'm doing wrong would be much appreciated, thanks! 关于我在做什么错的任何建议,将不胜感激,谢谢!

Is it because the element (eg $('#confirmation_dialog').find('.cancel') ) doesn't exist at the point you're binding to it? 是因为元素(例如$('#confirmation_dialog').find('.cancel') )在绑定到该点时不存在?

You can use jQuery's on method to bind to the event dynamically by specifying the selector, like so: 您可以通过指定选择器来使用jQuery的on方法动态绑定到事件,如下所示:

$('body').on 'click', '#confirmation_dialog .cancel', (ev) ->
    $('.disabled').removeClass('disabled')

This seems like a possible cause since dialogs are normally added to the page using JavaScript so don't exist in the DOM initially. 这似乎是可能的原因,因为通常使用JavaScript将对话框添加到页面中,因此最初在DOM中不存在。 Without seeing the full source though it's difficult to be sure. 尽管难以确定,但没有看到完整的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM