Jquery 确定用户是否选择';取消';在对话框中

Jquery 确定用户是否选择';取消';在对话框中,jquery,ruby-on-rails,Jquery,Ruby On Rails,在我的rails应用程序中,当用户尝试删除图像时,我会创建一个确认对话框: <%= link_to image, :method => :delete, :confirm=> "Are you sure you want to delete this video?", :remote => true, :class=> "btn btn-mini trash" do %> <i class="icon-trash"></i> &l

在我的rails应用程序中,当用户尝试删除图像时,我会创建一个确认对话框:

<%= link_to image, :method => :delete, :confirm=> "Are you sure you want to delete this video?", :remote => true, :class=> "btn btn-mini trash" do %>
   <i class="icon-trash"></i>
<% end %>
:delete,:confirm=>“确实要删除此视频吗?”,:remote=>true,:class=>“btn btn迷你垃圾”do%>

使用jquery如何检测用户是否从对话框窗口选择了“取消”?

此功能基于此,您可以非常轻松地进行自定义以满足您的需要,甚至可以基于Twitter引导或其他UI工具包进行完全自定义的对话框。有几种资源可用于这样做,以及

格雷厄姆为史蒂夫的文章提供了很好的链接

因此,根据文章和UJS代码,您可以覆盖UJS的
confirm
方法

原始代码很简单()

然后,使用香草Javascript,您可以在应用程序的js文件中编写类似的内容来覆盖此方法

$.rails.confirm = function(message) {
  if (confirm(message)) {
    return true;            //UJS will continue doing his job
  } else {         
    console.log('canceled') // Do you own logic
    return false;           //Make sure to return false at the end
  }     
}
JSFIDLE演示:

$.rails.confirm = function(message) {
  if (confirm(message)) {
    return true;            //UJS will continue doing his job
  } else {         
    console.log('canceled') // Do you own logic
    return false;           //Make sure to return false at the end
  }     
}