在我的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 %>
如何通过jquery检测用户是否从对话框窗口中选择“取消”?
答案 0 :(得分:2)
因此,根据文章和UJS代码,您可以覆盖UJS的confirm
方法。
原始代码很简单(https://github.com/rails/jquery-ujs/blob/master/src/rails.js)
// Default confirm dialog, may be overridden with custom
// confirm dialog in $.rails.confirm
confirm: function(message) {
return confirm(message);
},
然后,使用vanilla 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
}
}
jsfiddle demo:http://jsfiddle.net/billychan/GS5hZ/
答案 1 :(得分:0)
此功能基于Rails' UJS library,您可以轻松自定义以满足您的需求,甚至可以完成基于Twitter Bootstrap或其他UI工具包的自定义对话框。这样做有几种资源。 Here is a good post on this以及this Stack Overflow post。